• Upgrade your Internet Experience
  • Sign in
  • Microsoft.com
  • United States (English)
    Brasil (Português)Česká republika (Čeština)Deutschland (Deutsch)España (Español)France (Français)Italia (Italiano)Россия (Русский)대한민국 (한국어)中华人民共和国 (中文)台灣 (中文)日本 (日本語)香港特别行政區 (中文)
 
 
.NET Framework Developer Center
 
 
Home
 
 
Library
 
 
Learn
 
 
Downloads
 
 
Support
 
 
Community
 
 
Forums
 
 
 
.NET Framework Developer Center > .NET Development Forums > .NET Remoting and Runtime Serialization > Serialize a class Inheriting from an ISerializable Class
Ask a questionAsk a question
Search Forums:
  • Search .NET Remoting and Runtime Serialization Forum Search .NET Remoting and Runtime Serialization Forum
  • Search All .NET Development Forums Search All .NET Development Forums
  • Search All MSDN Forums Search All MSDN Forums
 

QuestionSerialize a class Inheriting from an ISerializable Class

  • Friday, September 29, 2006 10:10 AMMazzica Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    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.
    • ReplyReply
    • QuoteQuote
     

All Replies

  • Friday, September 29, 2006 11:52 AMLucian Bargaoanu Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    For no 1 add a constructor like

    Protected Sub New(ByVal si As SerializationInfo, ByVal context As StreamingContext)

    and call the base constructor with those parameters.

    • ReplyReply
    • QuoteQuote
     
Need Help with Forums? (FAQ)
 
© 2009 Microsoft Corporation. All rights reserved.
Terms of Use
|
Trademarks
|
Privacy Statement