Hi,
I want to serialize my custom class derived from Windows Forms TreeNode. TreeNode already Implements the ISerealizable interface
I already tried 2 solutions but didnt wok :
1) Override Serialize and Deserialize methods : didint work I got this exception The constructor to deserialize an object of type 'ConsoleApplication1.MyTreeNode' was not found. I dont know how to deserialize my Nodes back by defining this constructor. Then why the method Deserialize id not Called ? (I tested with a breakpoint) .this is my code
Code :
<Serializable()> Public Class MyTreeNode
Inherits TreeNode
Public m_id As Integer
Public Sub New()
End Sub
Public Sub New(ByVal id As Integer)
m_id = id
End Sub
Protected Overrides Sub Serialize(ByVal si As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext)
MyBase.Serialize(si, context)
si.AddValue("id", m_id)
End Sub
Protected Overrides Sub Deserialize(ByVal serializationInfo As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext)
MyBase.Deserialize(serializationInfo, context)
m_id = DirectCast(serializationInfo.GetValue("id", GetType(Integer)), Integer)
End Sub
End Class
2)Implementing ISerializable again and build the Serialization from scratch : this Solution is working well but got Exceptions when working with a Hierarchical TreeNode . this is my code :
Code :
<Serializable()> Public Class MyTreeNode
Inherits TreeNode
Implements ISerializable
Public m_id As Integer
Public Sub New()
End Sub
Public Sub New(ByVal id As Integer)
m_id=id
End Sub
Protected Sub New(ByVal si As SerializationInfo, ByVal context As StreamingContext)
Dim j As Integer = 1
m_id = CType(si.GetValue("id", GetType(Integer)), Integer)
Dim o As Object
Dim i As Integer = 1
While i <= (si.MemberCount - j)
o = si.GetValue(i.ToString, GetType(Object))
Nodes.Add(CType(o, TreeNode))
i += 1
End While
End Sub
Public Overridable Sub GetObjectData(ByVal si As SerializationInfo, ByVal context As StreamingContext)
si.AddValue("id", m_id)
Dim i As Integer = 1
For Each o As Object In Nodes
If o IsNot Nothing Then
si.AddValue(i.ToString, o)
i += 1
End If
Next
End Sub
End Class
Well, hope that I'll fix my issue from your answers . Thanks In Advance.