Click here to Skip to main content
15,888,454 members
Home / Discussions / C#
   

C#

 
GeneralRe: how to IF?! Pin
harold aptroot10-Apr-10 13:54
harold aptroot10-Apr-10 13:54 
GeneralRe: how to IF?! Pin
AspDotNetDev10-Apr-10 14:39
protectorAspDotNetDev10-Apr-10 14:39 
GeneralRe: how to IF?! Pin
harold aptroot10-Apr-10 14:48
harold aptroot10-Apr-10 14:48 
GeneralRe: how to IF?! Pin
AspDotNetDev10-Apr-10 15:02
protectorAspDotNetDev10-Apr-10 15:02 
GeneralRe: how to IF?! Pin
harold aptroot10-Apr-10 15:18
harold aptroot10-Apr-10 15:18 
GeneralRe: how to IF?! Pin
NavnathKale10-Apr-10 20:18
NavnathKale10-Apr-10 20:18 
GeneralRe: how to IF?! Pin
harold aptroot11-Apr-10 2:25
harold aptroot11-Apr-10 2:25 
QuestionSerialization problem Pin
Alan Balkany9-Apr-10 10:57
Alan Balkany9-Apr-10 10:57 
I'm having a hard time deserializing old versions of a class after adding a new member. I've written a toy program that's the simplest possible example of this problem. Maybe someone else might notice what I'm doing wrong.

I have a class with one member: A System.Drawing.Color:
[Serializable]
public class SerialObj
{
    public System.Drawing.Color color;
}


The following code serializes/deserializes it with no problem:
SoapFormatter formatter = new SoapFormatter();     // Serialize:
formatter.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
formatter.Serialize(myStream, so);

SoapFormatter formatter = new SoapFormatter();     // Deserialize:
formatter.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
so = (SerialObj)formatter.Deserialize(myStream);


I added an int member, and I'm trying to use the ISerializable interface to handle the different versions. This interface requires a constructor with two special parameters, and a GetObjectData method:
[Serializable]
public class SerialObj : ISerializable
{
    public System.Drawing.Color color;
    public int number;                   // The new member.

    public SerialObj()
    {
        System.Windows.Forms.MessageBox.Show("In empty ctor.");
    }


    public SerialObj(SerializationInfo info, StreamingContext cntxt)
    {
        System.Windows.Forms.MessageBox.Show("In full ctor.");
        color = (System.Drawing.Color)info.GetValue("color", typeof(System.Drawing.Color));

        try
        {
            number = info.GetInt32("number");
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
        }
    }


    void ISerializable.GetObjectData(SerializationInfo inf, StreamingContext cxt)
    {
        System.Windows.Forms.MessageBox.Show("In GetObjectData.");
        inf.AddValue("color", color);
        inf.AddValue("number", number);
    }
}


It works for serialized new versions with both members. But when I deserialize an old version (without the int), it crashes on the Deserialize call, with the exception: "Top Object cannot be instantiated for element 'color'." It never even calls either constructor.

Can anyone see what I'm doing wrong, or suggest an alternative approach?

Thanks!
Alan
AnswerRe: Serialization problem Pin
Migounette9-Apr-10 11:13
Migounette9-Apr-10 11:13 
GeneralRe: Serialization problem Pin
Alan Balkany9-Apr-10 11:17
Alan Balkany9-Apr-10 11:17 
GeneralRe: Serialization problem Pin
Alan Balkany9-Apr-10 11:38
Alan Balkany9-Apr-10 11:38 
GeneralRe: Serialization problem Pin
Migounette9-Apr-10 11:55
Migounette9-Apr-10 11:55 
AnswerRe: Serialization problem Pin
Luc Pattyn9-Apr-10 11:23
sitebuilderLuc Pattyn9-Apr-10 11:23 
GeneralRe: Serialization problem Pin
Alan Balkany9-Apr-10 11:29
Alan Balkany9-Apr-10 11:29 
QuestionDont understand arrays Pin
netJP12L9-Apr-10 10:44
netJP12L9-Apr-10 10:44 
AnswerRe: Dont understand arrays Pin
Migounette9-Apr-10 11:25
Migounette9-Apr-10 11:25 
AnswerRe: Dont understand arrays Pin
Sir Dot Net9-Apr-10 11:26
Sir Dot Net9-Apr-10 11:26 
AnswerRe: Dont understand arrays Pin
AspDotNetDev9-Apr-10 11:35
protectorAspDotNetDev9-Apr-10 11:35 
GeneralRe: Dont understand arrays Pin
Luc Pattyn9-Apr-10 12:24
sitebuilderLuc Pattyn9-Apr-10 12:24 
GeneralRe: Dont understand arrays Pin
AspDotNetDev9-Apr-10 16:12
protectorAspDotNetDev9-Apr-10 16:12 
Questionownerdrawn treenodes Pin
xilefxilef9-Apr-10 10:32
xilefxilef9-Apr-10 10:32 
AnswerRe: ownerdrawn treenodes Pin
NavnathKale9-Apr-10 11:10
NavnathKale9-Apr-10 11:10 
AnswerRe: ownerdrawn treenodes Pin
Migounette9-Apr-10 12:28
Migounette9-Apr-10 12:28 
GeneralRe: ownerdrawn treenodes Pin
xilefxilef9-Apr-10 13:59
xilefxilef9-Apr-10 13:59 
GeneralRe: ownerdrawn treenodes Pin
DaveyM6910-Apr-10 4:59
professionalDaveyM6910-Apr-10 4:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.