locked
Serialization Problem RRS feed

  • Question

  • Hi Guyz

    I have made a Class whose One Property is Serialized other are non-srializable as well as no need to serialize them

    When I serialize and deserialize it, I get expected result e.g. +ve results

    But other Class is Collection of This type e.g. Inherits List(Of MyObject)

    When I serialize this class, no error come

    But when deserialize it All the objects stored in it are already assigned to nothing

    <Serializable()>
    Public Class AtoObject
        Implements ISerializable
        Public Property Properties As New Dictionary(Of String, Object) 'This is Only To be serialized
        Default Public Property [Property](Name As String) As Object
            Get
                If Properties.Keys.ToList.IndexOf(Name) < 0 Then Return Nothing
                Return Properties(Name)
            End Get
            Set(value As Object)
                If Properties.Keys.ToList.IndexOf(Name) < 0 Then Properties.Add(Name, value) Else Properties(Name) = value
            End Set
        End Property
        Public Components As New List(Of AtoComponent) 'This must not be serialized
        Public aGame As AtoGame 'This too
        Public Overridable Sub Load(ByRef aGame As AtoGame)
            Me.aGame = aGame
            For Each c As AtoComponent In Components
                c.Load(aGame)
            Next
        End Sub
        Public Overridable Sub Draw(Sb As SpriteBatch)
            For Each c As AtoComponent In Components
                c.Draw()
            Next
        End Sub
        Public Overridable Sub Update()
            For Each c As AtoComponent In Components
                c.Update()
            Next
        End Sub
        Sub New()
    
        End Sub
        Protected Sub New(info As System.Runtime.Serialization.SerializationInfo, context As System.Runtime.Serialization.StreamingContext)
            Properties = DirectCast(info.GetValue("Properties", GetType(Dictionary(Of String, Object))), Dictionary(Of String, Object))
        End Sub
        Public Sub GetObjectData(info As System.Runtime.Serialization.SerializationInfo, context As System.Runtime.Serialization.StreamingContext) Implements System.Runtime.Serialization.ISerializable.GetObjectData
            info.AddValue("Properties", Properties)
        End Sub
    End Class

    This class works well but

    <Serializable()>
    Public Class AtoObjectCollection
        Inherits List(Of AtoObject)
        Implements ISerializable
    'Object Collection Stored in Should be serialized
        Public aGame As AtoGame 'This should not be serialized
        Public SpriteBatch As SpriteBatch 'This too
        Sub New()
    
        End Sub
        Protected Sub New(info As System.Runtime.Serialization.SerializationInfo, context As System.Runtime.Serialization.StreamingContext)
            Me.AddRange(DirectCast(info.GetValue("ABCObjects", GetType(List(Of AtoObject))), List(Of AtoObject)))
        End Sub
        Public Overridable Sub Load(ByRef aGame As AtoGame)
            Me.aGame = aGame
            SpriteBatch = New SpriteBatch(aGame.Game.GraphicsDevice)
        End Sub
        Public Overridable Sub Draw()
            SpriteBatch.Begin()
            For Each Obj As AtoObject In Me
                Obj.Draw(SpriteBatch)
            Next
            SpriteBatch.End()
        End Sub
        Public Overridable Sub Update()
            For Each Obj As AtoObject In Me
                Obj.Update()
            Next
        End Sub
        Public Sub GetObjectData(info As System.Runtime.Serialization.SerializationInfo, context As System.Runtime.Serialization.StreamingContext) Implements System.Runtime.Serialization.ISerializable.GetObjectData
            info.AddValue("ABCObjects", Me.ToList)
        End Sub
    End Class

    But after deserialization this object of this type has all null objects

    Can You Please Help me?


    ---------------------Do the Impossible--------------------- Great Software at http://atosoft.webs.com/

    Tuesday, May 14, 2013 6:22 AM

Answers

  • What were the specific error details?  There are some diiferences between binary and XML serialization that may require changes in the classes.

    Wouldn't it be easier to make a List<T> public property, instead of inheriting List<T>?  That should serialize and deserialize without issues.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.com/

    • Marked as answer by NaxAlpha Wednesday, May 15, 2013 4:57 AM
    Tuesday, May 14, 2013 5:12 PM

All replies

  • Do you have code which does the deserialization. I see nothing of that in your code.

    Success
    Cor

    Tuesday, May 14, 2013 7:59 AM
  •     Sub Serialize(Obj As Object, File As String)
            Dim bf As New BinaryFormatter
            Using sw As New StreamWriter(File)
                bf.Serialize(sw.BaseStream, Obj)
            End Using
        End Sub
        Function Deserialize(Of T)(File As String) As T
            Dim f As New BinaryFormatter
            Using sr As New StreamReader(File)
                Return DirectCast(f.Deserialize(sr.BaseStream), T)
            End Using
        End Function

    Plz Help Me


    ---------------------Do the Impossible--------------------- Great Software at http://atosoft.webs.com/

    Tuesday, May 14, 2013 9:00 AM
  • T is a placeholder for the Type put the correct class in it. (Atoobject I guess)

    Look at this sample on our website.

    http://www.vb-tips.com/SerializeGernericListToFile.ASPX


    Success
    Cor

    Tuesday, May 14, 2013 9:50 AM
  • Nothing

    ---------------------Do the Impossible--------------------- Great Software at http://atosoft.webs.com/

    Tuesday, May 14, 2013 11:46 AM
  • Try using the XML serializer so that you easily read what exactly is written out to the file.

    Hope this helps.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.com/

    Tuesday, May 14, 2013 2:06 PM

  • ---------------------Do the Impossible--------------------- Great Software at http://atosoft.webs.com/

    Tuesday, May 14, 2013 5:01 PM
  • What were the specific error details?  There are some diiferences between binary and XML serialization that may require changes in the classes.

    Wouldn't it be easier to make a List<T> public property, instead of inheriting List<T>?  That should serialize and deserialize without issues.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.com/

    • Marked as answer by NaxAlpha Wednesday, May 15, 2013 4:57 AM
    Tuesday, May 14, 2013 5:12 PM
  • What kind of objects are stored in the "Properties" dictionary when the code fails?  If you add an object to the dictionary which cannot be serialized, the entire serialization process will fail.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, May 14, 2013 7:01 PM
  • Hi Rudedog2,

    Making Public Property makes Time Wastage

    Like If we have to Write Objects.Add we must write Objects.Objects.Add

    also that if we want to write Objects(0)=Nothing we must write Objects.Objects(0)=Nothing etc

    Hi Reed,

    I have added Integers in Objects, When I serialized or deserialized Only Alone AtoObject it returned as i wanted but when I add one or 2 objects in collection it gives one or 2 Nothing Objects

    I have used break points on GetObjectData and Protected Sub New,After Getting Object Data,On End Sub of GetObjectData I added Breakpoint and examin the values of Info Then there All Object exists, But when sub new is called I added break point before assigning values of Objects, I watched that Info has all Nothing???

    I also used Try Statement and I caught no exception

    Very Strange...

    Can Anyone help me to get out of this trouble


    ---------------------Do the Impossible--------------------- Great Software at http://atosoft.webs.com/



    • Edited by NaxAlpha Wednesday, May 15, 2013 3:22 AM
    Wednesday, May 15, 2013 3:19 AM
  • I have this to test serialization or deserialization

                Dim x As New AtoObject
                x("123") = 15
                x("abc") = 11
                Dim y As New AtoObjectCollection
                y.Add(x)
                y.Add(x)
                Serialize(y, "2.txt")
                y = New AtoObjectCollection
                y = Deserialize(Of AtoObjectCollection)("2.txt")

    Plz help me


    ---------------------Do the Impossible--------------------- Great Software at http://atosoft.webs.com/

    Wednesday, May 15, 2013 3:24 AM
  • I have Used Implement IList(Of AtoObject)

    And Private Collection As New List(Of AtoObject)

    I filled All Implemented function and It worked Very Well by Serializing only Collection

    Thanks for helping But I still want to know What the reason that It doesn't Serialize My Class

    If You know any reason Please post it.


    ---------------------Do the Impossible--------------------- Great Software at http://atosoft.webs.com/

    Wednesday, May 15, 2013 4:56 AM