473,698 Members | 2,551 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TypeConverter Woes

Hello,

I'm writing a TypeConverter for a class which inherits arraylist, but
deserializing it always results in an arraylist, which causes an
InvalidCastExce ption. If anyone can point out what I'm doing wrong here,
I'd really appreciate it.

The class:

<TypeConverter( GetType(CXMLObj ectListConverte r))> _
Public Class CXMLObjectList
Inherits ArrayList

#Region "Constants"
Private Const XML_ATTRIBUTE_C ount As String = "Count"
Private Const XML_ATTRIBUTE_C lassName As String = "ClassName"
#End Region

#Region "New()"
Public Sub New()
MyBase.New()
End Sub
#End Region

#Region "New(ByVal RootElement As String)"
Public Sub New(ByVal RootElement As String)
MyBase.New()
mRootElement = RootElement
End Sub
#End Region

#Region "New(ByVal RootElement As String, ByVal TotalCount As Integer)"
Public Sub New(ByVal RootElement As String, ByVal TotalCount As
Integer)
MyBase.New()
mRootElement = RootElement
mTotalCount = TotalCount
End Sub
#End Region

#Region "New(ByVal xDoc As XmlDocument)"
Public Sub New(ByVal xDoc As XmlDocument)
Me.New(xDoc.Chi ldNodes(0))
RootElement = xDoc.ChildNodes (0).Value
End Sub
#End Region

#Region "New(ByVal xNode As XmlNode)"
Public Sub New(ByVal xNode As XmlNode)
Dim childValue As String
TotalCount = CInt(xNode.Attr ibutes(XML_ATTR IBUTE_Count).Va lue)
For Each childNode As XmlNode In xNode.ChildNode s
If xNode.Item(chil dNode.Name).Has ChildNodes Then
Dim myType As Type =
Type.GetType(ch ildNode.Attribu tes(XML_ATTRIBU TE_ClassName).V alue)
Dim types(0) As Type
types(0) = GetType(XmlNode )
' Get the public instance constructor for the loaded type
Dim constructorInfo Obj As ConstructorInfo = _
myType.GetConst ructor(BindingF lags.Instance
Or _
BindingFlags.Pu blic, Nothing, _
CallingConventi ons.HasThis, types, Nothing)
If Not (constructorInf oObj Is Nothing) Then
Dim parms(0) As Object
parms(0) = CType(childNode , XmlNode)
Me.Add(construc torInfoObj.Invo ke(parms))
End If
End If
Next
End Sub
#End Region

#Region "Members"
Private mRootElement As String
Private mTotalCount As Integer
#End Region

#Region "Properties "
Public Property TotalCount() As Integer
Get
If mTotalCount = 0 Then
mTotalCount = Me.Count
End If
Return mTotalCount
End Get
Set(ByVal Value As Integer)
mTotalCount = Value
End Set
End Property

Public Property RootElement() As String
Get
Return mRootElement
End Get
Set(ByVal Value As String)
mRootElement = Value
End Set
End Property
#End Region

#Region "toXMLDocum ent"
Public Function toXMLDocument() As XmlDocument
Return toXMLDocument(N othing)
End Function

Private Function toXMLDocument(B yVal objectList As ArrayList) As
XmlDocument
Dim xmlStream As New MemoryStream
Dim xml As New XmlTextWriter(x mlStream, Encoding.UTF8)
xml.WriteStartD ocument()
xml.WriteStartE lement(RootElem ent)
xml.WriteAttrib uteString(XML_A TTRIBUTE_Count, TotalCount.ToSt ring)
If IsNothing(objec tList) Then
objectList = Me
End If
For Each xObject As CXMLObject In objectList
xObject.toXML(x ml)
Next
xml.WriteEndEle ment()
xml.WriteEndDoc ument()
xml.Flush()
xmlStream.Seek( 0, SeekOrigin.Begi n)
Dim xmldoc As New XmlDocument
xmldoc.Load(xml Stream)
Return xmldoc
End Function

#End Region

End Class

The TypeConverter:

Public Class CXMLObjectListC onverter
Inherits TypeConverter

Public Overloads Overrides Function CanConvertFrom( ByVal context As
System.Componen tModel.ITypeDes criptorContext, ByVal sourceType As
System.Type) As Boolean
If sourceType Is GetType(XmlDocu ment) OrElse sourceType Is
GetType(XmlNode ) Then
Return True
End If
If sourceType Is GetType(String) Then
Return True
End If
Return MyBase.CanConve rtFrom(context, sourceType)
End Function

Public Overloads Overrides Function ConvertFrom(ByV al context As
System.Componen tModel.ITypeDes criptorContext, ByVal culture As
System.Globaliz ation.CultureIn fo, ByVal value As Object) As Object
If TypeOf value Is XmlDocument OrElse value Is GetType(XmlNode )
Then
Return New CXMLObjectList( CType(value, XmlNode))
End If
If TypeOf value Is String Then
Dim xDoc As New XmlDocument
xDoc.LoadXml(CS tr(value))
Return New CXMLObjectList( xDoc)
End If
Return MyBase.ConvertF rom(context, culture, value)
End Function

Public Overloads Overrides Function CanConvertTo(By Val context As
System.Componen tModel.ITypeDes criptorContext, ByVal destinationType As
System.Type) As Boolean
If destinationType Is GetType(XmlDocu ment) OrElse
destinationType Is GetType(XmlNode ) Then
Return True
End If
If destinationType Is GetType(String) Then
Return True
End If
Return MyBase.CanConve rtTo(context, destinationType )
End Function

Public Overloads Overrides Function ConvertTo(ByVal context As
System.Componen tModel.ITypeDes criptorContext, ByVal culture As
System.Globaliz ation.CultureIn fo, ByVal value As Object, ByVal
destinationType As System.Type) As Object
If destinationType Is GetType(XmlDocu ment) OrElse
destinationType Is GetType(XmlNode ) Then
Return CType(value, CXMLObjectList) .toXMLDocument
End If
If destinationType Is GetType(String) Then
Return CType(value, CXMLObjectList) .toXMLDocument. ToString
End If
Return MyBase.ConvertT o(context, culture, value, destinationType )
End Function
End Class

The Code I'm using to test:

Dim list As New CXMLObjectList
Dim los As New LosFormatter
Dim sw As New StringWriter
los.Serialize(s w, list)
list = Nothing
list = los.Deserialize (sw.GetStringBu ilder.ToString)

Thanks in advance,
-Eric
Nov 21 '05 #1
0 1210

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2192
by: João Santa Bárbara | last post by:
Hi all i have a class that i have made and i made an Typeconverter as well, and my problem is the code genereated in my form is Dim ClassTest1 As TestApplication.ClassTest = New TestApplication.ClassTest Me.MyClassTest = ClassTest1 Wich is quite good, but my problem is in my property browser i cannot change any of my properties
2
3414
by: kw | last post by:
I'm getting different behavior from what I would expect and was hoping someone could clue me in. At run time I need to examine a property of an object for a custom TypeConverter, then use that converter on a value. Here's the deal: object attrs=(object)PropertyInfo.GetCustomAttributes(typeof(TypeConverterAttribu te),false);
1
1698
by: Sky Sigal | last post by:
(PS: Cross post from microsoft.pulic.dotnet.framework.aspnet.webcontrols) I've been looking lately for a way to keep the Properties panel for Controls 'clean'... My goal is to keep similar public properties of a custom Control neatly tied together -- rather than all over the IDE. One such set of values that will rarely be changed, so should have little priority in the IDE Properties panel, and therefore a good candidate for
5
2184
by: ljlevend | last post by:
Is there a TypeConverter that converts Doubles to percent values in a PropertyGrid? The Windows.Forms.Form.Opacity property seems to use the TypeConverter that I want. Thank you, Lance
4
2171
by: Kent Boogaart | last post by:
*tried this already on the buildingcontrols newsgroup but didn't get a response* Hi, As far as I can tell, it is not possible to use the TypeConverter infrastructure with generic types. Say you have this type: public struct Id<T> where T : IComparable { ... }
4
1700
by: swartzbill2000 | last post by:
Hello, I have a TypeConverter for converting between this Enum and Strings. Public Enum DeviceNameEnum dnNone dn2500 dnMirror End Enum 'DeviceNameEnum
11
2338
by: Rolf Welskes | last post by:
Hello, the problem seems to be complex and is in all developments of web-controls which uses own TypeConverter. For this I have here a simple demo-program of the problem: The Control-code: A class MyString which is a class which is similar to a string public class MyString {
1
2154
by: --== Alain ==-- | last post by:
Hi, I 'm facing an interesting issue regarding a property and its TypeConverter. When i do not attach a TypeConverter to this property, all custom properties of my custom control are displayed in Test Container. Since i attached this TypeConverter to my property, ALL custom properties (properties implemented by myself) are not displayed in Test Container.
6
3890
by: Larry Smith | last post by:
Hi there, Can anyone provide any insight on why MSFT introduced "TypeConverter.GetProperties()". There are two classes for dealing with metadata in .NET, 'Type" and "TypeDescriptor". Each has a "GetProperites()" method so why complicate the situation even more than it already is by adding a "GetProperties()" method to "TypeConverter". The class is supposed to be for converting types so "GetProperties()" seems misplaced and redundant (as...
0
9169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7738
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.