473,421 Members | 1,469 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,421 software developers and data experts.

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
InvalidCastException. If anyone can point out what I'm doing wrong here,
I'd really appreciate it.

The class:

<TypeConverter(GetType(CXMLObjectListConverter)) > _
Public Class CXMLObjectList
Inherits ArrayList

#Region "Constants"
Private Const XML_ATTRIBUTE_Count As String = "Count"
Private Const XML_ATTRIBUTE_ClassName 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.ChildNodes(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.Attributes(XML_ATTRIBUTE_Count).Value)
For Each childNode As XmlNode In xNode.ChildNodes
If xNode.Item(childNode.Name).HasChildNodes Then
Dim myType As Type =
Type.GetType(childNode.Attributes(XML_ATTRIBUTE_Cl assName).Value)
Dim types(0) As Type
types(0) = GetType(XmlNode)
' Get the public instance constructor for the loaded type
Dim constructorInfoObj As ConstructorInfo = _
myType.GetConstructor(BindingFlags.Instance
Or _
BindingFlags.Public, Nothing, _
CallingConventions.HasThis, types, Nothing)
If Not (constructorInfoObj Is Nothing) Then
Dim parms(0) As Object
parms(0) = CType(childNode, XmlNode)
Me.Add(constructorInfoObj.Invoke(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 "toXMLDocument"
Public Function toXMLDocument() As XmlDocument
Return toXMLDocument(Nothing)
End Function

Private Function toXMLDocument(ByVal objectList As ArrayList) As
XmlDocument
Dim xmlStream As New MemoryStream
Dim xml As New XmlTextWriter(xmlStream, Encoding.UTF8)
xml.WriteStartDocument()
xml.WriteStartElement(RootElement)
xml.WriteAttributeString(XML_ATTRIBUTE_Count, TotalCount.ToString)
If IsNothing(objectList) Then
objectList = Me
End If
For Each xObject As CXMLObject In objectList
xObject.toXML(xml)
Next
xml.WriteEndElement()
xml.WriteEndDocument()
xml.Flush()
xmlStream.Seek(0, SeekOrigin.Begin)
Dim xmldoc As New XmlDocument
xmldoc.Load(xmlStream)
Return xmldoc
End Function

#End Region

End Class

The TypeConverter:

Public Class CXMLObjectListConverter
Inherits TypeConverter

Public Overloads Overrides Function CanConvertFrom(ByVal context As
System.ComponentModel.ITypeDescriptorContext, ByVal sourceType As
System.Type) As Boolean
If sourceType Is GetType(XmlDocument) OrElse sourceType Is
GetType(XmlNode) Then
Return True
End If
If sourceType Is GetType(String) Then
Return True
End If
Return MyBase.CanConvertFrom(context, sourceType)
End Function

Public Overloads Overrides Function ConvertFrom(ByVal context As
System.ComponentModel.ITypeDescriptorContext, ByVal culture As
System.Globalization.CultureInfo, 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(CStr(value))
Return New CXMLObjectList(xDoc)
End If
Return MyBase.ConvertFrom(context, culture, value)
End Function

Public Overloads Overrides Function CanConvertTo(ByVal context As
System.ComponentModel.ITypeDescriptorContext, ByVal destinationType As
System.Type) As Boolean
If destinationType Is GetType(XmlDocument) OrElse
destinationType Is GetType(XmlNode) Then
Return True
End If
If destinationType Is GetType(String) Then
Return True
End If
Return MyBase.CanConvertTo(context, destinationType)
End Function

Public Overloads Overrides Function ConvertTo(ByVal context As
System.ComponentModel.ITypeDescriptorContext, ByVal culture As
System.Globalization.CultureInfo, ByVal value As Object, ByVal
destinationType As System.Type) As Object
If destinationType Is GetType(XmlDocument) 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.ConvertTo(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(sw, list)
list = Nothing
list = los.Deserialize(sw.GetStringBuilder.ToString)

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

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

Similar topics

2
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...
2
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...
1
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...
5
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
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...
4
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
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...
1
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...
6
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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...

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.