Click here to Skip to main content
15,910,603 members
Home / Discussions / C#
   

C#

 
GeneralRe: how to IF?! Pin
Jassim Rahma9-Apr-10 12:30
Jassim Rahma9-Apr-10 12:30 
GeneralRe: how to IF?! Pin
harold aptroot9-Apr-10 12:31
harold aptroot9-Apr-10 12:31 
GeneralRe: how to IF?! Pin
Jassim Rahma9-Apr-10 12:36
Jassim Rahma9-Apr-10 12:36 
AnswerRe: how to IF?! Pin
AspDotNetDev9-Apr-10 16:18
protectorAspDotNetDev9-Apr-10 16:18 
GeneralRe: how to IF?! Pin
harold aptroot10-Apr-10 1:04
harold aptroot10-Apr-10 1:04 
GeneralRe: how to IF?! Pin
Luc Pattyn10-Apr-10 1:36
sitebuilderLuc Pattyn10-Apr-10 1:36 
JokeRe: how to IF?! Pin
AspDotNetDev10-Apr-10 10:40
protectorAspDotNetDev10-Apr-10 10:40 
GeneralRe: how to IF?! Pin
Luc Pattyn10-Apr-10 11:19
sitebuilderLuc Pattyn10-Apr-10 11:19 
GeneralRe: how to IF?! Pin
AspDotNetDev10-Apr-10 11:29
protectorAspDotNetDev10-Apr-10 11:29 
GeneralRe: how to IF?! Pin
Luc Pattyn10-Apr-10 11:46
sitebuilderLuc Pattyn10-Apr-10 11:46 
GeneralRe: how to IF?! Pin
AspDotNetDev10-Apr-10 13:32
protectorAspDotNetDev10-Apr-10 13:32 
GeneralRe: how to IF?! Pin
Luc Pattyn10-Apr-10 13:51
sitebuilderLuc Pattyn10-Apr-10 13:51 
GeneralRe: how to IF?! Pin
AspDotNetDev10-Apr-10 14:34
protectorAspDotNetDev10-Apr-10 14:34 
GeneralRe: how to IF?! Pin
PIEBALDconsult19-Apr-10 13:01
mvePIEBALDconsult19-Apr-10 13:01 
AnswerRe: how to IF?! [modified] Pin
NavnathKale10-Apr-10 2:43
NavnathKale10-Apr-10 2:43 
GeneralRe: how to IF?! Pin
harold aptroot10-Apr-10 3:03
harold aptroot10-Apr-10 3:03 
GeneralRe: how to IF?! Pin
AspDotNetDev10-Apr-10 13:35
protectorAspDotNetDev10-Apr-10 13:35 
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

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.