|
-
May 15th, 2008, 02:28 PM
#1
Thread Starter
Member
[2005] Deserialization and Late Binding
I have an object that I am deserializing. The object is a Generic.List and it could contain one of two different objects, so how do I find what objects are in the List so that I can use directcast. I'm tying to find a solution that doesn't use late binding. I think the solution involves reflection but I don't know how to do it.
-
May 15th, 2008, 02:55 PM
#2
Re: [2005] Deserialization and Late Binding
If you use xml serialization, then you can just take a look at the nodes and see what's in there. Here's a little code snippet I whipped up to show how it could be done. Each list seems to get serialized with a root node of <ArrayOf{Object}>, so you can just pull that out and grab the type based on what it is.
Code:
Private Sub btnDeserialize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeserialize.Click
Dim lst1 As New List(Of String)
lst1.Add("Hello")
lst1.Add("Goodbye")
SerializeXML(lst1, "c:\string.xml")
Dim lst2 As New List(Of Integer)
lst2.Add(4)
lst2.Add(6)
SerializeXML(lst2, "c:\integer.xml")
Dim lst3 As New List(Of Person)
Dim p1 As New Person
p1.FirstName = "Peter"
p1.LastName = "Higgins"
Dim p2 As New Person
p2.FirstName = "Tom"
p2.LastName = "Sawyer"
lst3.Add(p1)
lst3.Add(p2)
SerializeXML(lst3, "c:\person.xml")
MessageBox.Show(DeserializeXML("c:\string.xml"))
MessageBox.Show(DeserializeXML("c:\integer.xml"))
MessageBox.Show(DeserializeXML("c:\person.xml"))
End Sub
Private Sub SerializeXML(ByVal obj As Object, ByVal fileName As String)
Dim serializer As New Xml.Serialization.XmlSerializer(obj.GetType)
Dim memStrm As New System.IO.MemoryStream
serializer.Serialize(memStrm, obj)
memStrm.Position = 0
Dim doc As New XmlDocument()
doc.Load(memStrm)
doc.Save(fileName)
memStrm.Close()
End Sub
Private Function DeserializeXML(ByVal fileName As String) As String
Dim doc As New XmlDocument
doc.Load(fileName)
Return doc.DocumentElement.Name.Replace("ArrayOf", "")
End Function
(VB/C#) is clearly superior to (C#/VB) because it (has/doesn't have) <insert trivial difference here>.
-
May 15th, 2008, 03:36 PM
#3
Re: [2005] Deserialization and Late Binding
if it is a generic list of whatever, then calling gettype on one of the lists elements will give you the the specific type of the elements inside...
something like this:
Code:
Public Function GetGenericTypeString() As String
If _GenericList.Item(0).GetType.Equals(GetType(System.Int32)) Then
Return "INTEGER"
ElseIf _GenericList.Item(0).GetType.Equals(GetType(System.Double)) Then
Return "DOUBLE"
Else
Return String.Empty
End If
End Function
however it sort of defeats the purpose of generics when you need the generic class to be aware of specific types... you might be better off with a non generic class that just has overloads for the 2 different types...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|