SoapFormatter, Serialization of a DictionaryBase derived class - Please help

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: a (a_at_a)
Date: 03/01/04


Date: Mon, 1 Mar 2004 11:14:53 -0000

Hi,

I am using a SoapFormatter to serialize a class which has a member that is
derived from DictionaryBase.

Everything works fine... I can serialize and deserialize. The trouble I
have is that I need to implement versioning, so I have to implement the
ISerializable interface and provide the GetObjectData function and the
constructor to deserialize the object. When I try to deserialize the member
that is derived from DictionaryBase, I get an empty object.

Here is some sample code:

<Serializable()> _
Public Class clsSimpleDictionary
    Inherits DictionaryBase
        '
        ' I have removed all the code just to be concise
        '
End Class

<Serializable()> _
Public Class SerializedSettings
    Implements ISerializable

    Public m_SMTPServer As String
    Public m_AddressBook As clsSimpleDictionary

    Public Sub New()
        m_AddressBook = New clsSimpleDictionary
    End Sub

    Protected Sub New(ByVal info As SerializationInfo, ByVal context As
StreamingContext)
        Try
            m_SMTPServer = info.GetString("m_SMTPServer")

            '
            ' This is the offending line
            '
            m_AddressBook = info.GetValue("m_AddressBook",
GetType(clsSimpleDictionary))
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Sub

    Private Sub GetObjectData(ByVal info As SerializationInfo, ByVal context
As StreamingContext)
        Implements ISerializable.GetObjectData

        info.AddValue("m_SMTPServer", m_SMTPServer, m_SMTPServer.GetType())
        info.AddValue("m_AddressBook", m_AddressBook,
m_AddressBook.GetType())
    End Sub

End Class

If I don't implement ISerializable, the SoapFormatter will serialize and
deserialize the clsSimpleDictionary member fine.

Can somebody please point me in the right direction.

TIA

Allan



Relevant Pages

  • Re: version compatibility when serializing and deserializing
    ... to deserialize an object that lacks that member, ... file with the serialized object and I didn't get any exception. ... You would need to serialize an ...
    (microsoft.public.dotnet.languages.csharp)
  • SoapFormatter, Serialization of a DictionaryBase derived class - Please help
    ... I am using a SoapFormatter to serialize a class which has a member that is ... I can serialize and deserialize. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: version compatibility when serializing and deserializing
    ... deserialize an object that has been serialized by an earlier version of your ... Specilically if you add a member to a custom class and attempt ... public int productId = 13; ... You would need to serialize an instance with the name field present. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: version compatibility when serializing and deserializing
    ... As a side note you may want also to check http://msdn.microsoft.com/en-us/library/ms229752.aspx (Version Tolerant Serialization). ... Note thought that instead of analyzing the text word by word and even if you don't have any exception, it still could cause issues in the application if it is not prepared to deal with this. ... So keep just in mind that the basic issue is that you are either loosing some information or you add new information with default values as soon as you don't deserialize exactly what was serialized. ... "You might have version compatibility issues if you ever attemp to deserialize an object that has been serialized by an earlier version of your application, Specilically if you add a member to a custom class and attempt to deserialize an object that lacks that member, the runtime will throw an exeption. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: version compatibility when serializing and deserializing
    ... deserialize exactly what was serialized. ... attempt to deserialize an object that lacks that member, ... the runtime will throw an exeption" will not throw any exception for me.I ... public int productId = 13; ...
    (microsoft.public.dotnet.languages.csharp)