Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C# Serialization issue (SerializationInfo )

395 views
Skip to first unread message

Marc Forand

unread,
Oct 18, 2002, 10:42:02 AM10/18/02
to
Hello,

I'm migrating from VS6 to .NET. Presently working on serialization.

The problem:
Since basic serialization doesn't allocate memory for objects, I must use
custom serialization. Fine, but the object SerializationInfo class needs a
name for a value to be added to it. The problem is that it doesn't accept a
same name for 2 values.

Say that I have a MyClass1 to serialize. If I have multiple MyClass1 to
serialize (but not an array), there will be many MyClass1 properties. I
can't believe I must implement a function to set a unique name for all the
MyClass1 I will encounter in my app.

There must be a way to get around or something like the serialization
process in MFC (but without using C++), which worked just fine to me !

Any help would be very appreciated !!!!

Thanks in advance !!

Marc


Nicholas Paldino [.NET/C# MVP]

unread,
Oct 18, 2002, 10:46:37 AM10/18/02
to
Marc,

Well, how do you store all the instances of MyClass1? If you have them
all stored in different fields on the class, then why can't you use the
field names as the names for your values?

Or, why not just use an array of these and then serialize the array?

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- nicholas...@exisconsulting.com

"Marc Forand" <marc....@videotron.ca> wrote in message
news:V6Vr9.11575$Mi5.1...@weber.videotron.net...

ozone@yahoo.com Jed Ozone

unread,
Oct 18, 2002, 11:12:57 AM10/18/02
to
..serialize with...
info.AddValue("MyFirstObject", _firstobject);
info.AddValue("MySecondObject", _secondobject);

...deserialize with...
_firstobject = info.GetValue("MyFirstObject", typeof(MyClass1));
_secondobject = info.GetValue("MySecondObject", typeof(MyClass1));

"Marc Forand" <marc....@videotron.ca> wrote in message
news:V6Vr9.11575$Mi5.1...@weber.videotron.net...

Marc Forand

unread,
Oct 18, 2002, 12:02:12 PM10/18/02
to
Thank you very much for your answers, Jed and Nicholas

Jed, the syntax you are giving me is what I'm using but I get the runtime
error listed a few lines below, near the end.

Nicholas, your questions are relevant but here are the answers:

1) How do I store my instances ?

Say I have a class MyPoint (having x, y and z) that is used by many classes,
say MyLine, MyCircle and MySquare. I need each of these later classes to
serialize itself and their own MyPoint instances. I can't manage to make an
array of all MyPoint and then serialize that array. It would be to much of a
mess to implement.

2) Why can't I use the field name ?

Here is the syntax for the SerializeInfo Class that is needed for custom
serialization:

// Serialization function (overridden from ISerializable).

public void GetObjectData(SerializationInfo SerInf, StreamingContext
context)

{

SerInf.AddValue("x", x);

SerInf.AddValue("y", y);

SerInf.AddValue("z", z);

}

As you can see, the syntax for AddValue is (String name, Object Obj). Now if
I call the serialize function for MyLine, it will call the serialization
function (GetObjectData) for the nested MyPoint. So "x", "y" and "z" will be
assigned in the SerializationInfo Class.

The next MyPoint being serialized is asking SerializationInfo to add a value
(a MyPoint) also named "x", "y" and "z" which were already assigned to the
first point. Unless I got it all wrong, the runtime error I'm having tells
me that SerializationInfo can't have 2 values having the same name. Here is
the message I get when I run:

----------------

An unhandled exception of type
'System.Runtime.Serialization.SerializationException' occurred in
mscorlib.dll

Additional information: Cannot add the same member twice to a
SerializationInfo object.

----------------

I assume that "add the same member twice" means giving the same name ("x"
for instance) for 2 different MyPoint. I find this unbelievable....Is there
anything like the former MFC where I could serialize sequentially objects
(without names) then deserialize it sequentially ?

Any other clue ?

Thanks again !

Marc

-----Message d'origine-----

De : Nicholas Paldino [mailto:nicholas...@exisconsulting.com]

Envoyé : 18 octobre, 2002 10:54

À : marc....@videotron.ca

Objet : Re: C# Serialization issue (SerializationInfo )

Marc,

Well, how do you store all the instances of MyClass1? If you have them

all stored in different fields on the class, then why can't you use the

field names as the names for your values?

Or, why not just use an array of these and then serialize the array?

Hope this helps.

--

- Nicholas Paldino [.NET/C# MVP]

- nicholas...@exisconsulting.com

"Marc Forand" <marc....@videotron.ca> wrote in message

news:V6Vr9.11575$Mi5.1...@weber.videotron.net...

Nicholas Paldino [.NET/C# MVP]

unread,
Oct 18, 2002, 12:05:27 PM10/18/02
to
Marc,

Ahh, so you want to serialized shared instances of objects, right? x, y
and z are all shared between instances and you want to maintain that fact
after serialization.

I don't know if .NET supports that if you are just serializing one
instance at a time. Since this is the case, I suggest putting all of the
objects that contain x, y, and z into another container object, and
serializing that. This way, the references between all of them should be
maintained.


--
- Nicholas Paldino [.NET/C# MVP]
- nicholas...@exisconsulting.com


"Marc Forand" <marc....@videotron.ca> wrote in message

news:4iWr9.11980$Mi5.1...@weber.videotron.net...

ozone@yahoo.com Jed Ozone

unread,
Oct 18, 2002, 1:36:52 PM10/18/02
to
I'm not sure if Nicholas or I am misunderstanding. Probably me because I
don't think I understand why you need to do things they way your are. So
excuse the code below, just trying to clarify things.

I'm not sure why you can't use (in your case) the attribute [Serializable]
on your class and let .NET deal with all the serialization code (no
ISerializable interface). This is the equivalent to the MFC serialization
(just less work in .NET).

[Serializable]
public class MyCircle
{
MyPoint center;
MyPoint outer;
}

[Serializable]
public class MyPoint
{ ... }

Then to serialize (I'm sure you already know this, but just to be clear
since I'm a bit confused)...

MyCircle myobject = new MyCircle();
FileStream fs = new FileStream("savemydata.bin", FileMode.Create);
BinaryFormatter b = new BinaryFormatter();
b.Serialize(fs, myobject);
fs.Close();

To deserialize...

MyCircle myobject = new MyCircle();
FileStream fs = new FileStream("savemydata.bin", FileMode.Open);
BinaryFormatter b = new BinaryFormatter();
MyCircle myobject = (MyCircle)b.Deserialize(fs);
fs.Close();

I never tested this, but I assume this works (by the way, I just hand typed
the above code in, so there might be typos).


"Marc Forand" <marc....@videotron.ca> wrote in message

news:4iWr9.11980$Mi5.1...@weber.videotron.net...

Marc Forand

unread,
Oct 18, 2002, 2:52:36 PM10/18/02
to
Thank you very much for the time you are taking Jed.

Well...I ran some tests with your comments...It worked just fine. The thing
I missed is that I'm not used to program without pointers... .NET does not
use them anymore. I was just wondering how my app could possibly know what
it has to serialize without telling it what to serialize (as I had to do in
MFC VS6)... Anyway !!!

It worked !!!

Thanks again Jed and all of you who joined !!!!
Hope to help you in my turn some time !!!

Marc

"Jed Ozone" <jed oz...@yahoo.com> wrote in message
news:uTih8xsdCHA.2244@tkmsftngp11...

ozone@yahoo.com Jed Ozone

unread,
Oct 18, 2002, 3:33:17 PM10/18/02
to
By the way, if you have a element you don't want serialized in a class, you
can just put the [NonSerialized] attribute in front:

[Serializable]
public class MyCircle
{
MyPoint center;

[NonSerialized] MyPoint outer; // The member will not get serialized
}

Serializing does have the downside that it doesn't have versioning (as far
as I know). So you have to be careful. The only way I could figure out to
do versioning was with ISerialize interface (which as you discovered is a
lot more work). So if your product isn't fairly stable, and you will be
changing things often, [Serializable] does have it's downside.

There are ways around your previous problem. For example, instead of
serializing your MyPoint objects, you could have stored the MyPoint
objects' data in your MyCircle serialization. Then you just recreate the
MyPoint objects while deserializing MyCircle object. You could also stick
all your MyPoint objects into a ArrayList and then serialize the ArrayList.
This is probably the easier solution if you have many objects of the same
type and your using ISerialize interface.

"Marc Forand" <marc....@videotron.ca> wrote in message

news:QNYr9.12503$Mi5.1...@weber.videotron.net...

Bryon Lape

unread,
Oct 18, 2002, 7:05:39 PM10/18/02
to
Marc Forand wrote:

> The problem:
> Since basic serialization doesn't allocate memory for objects,

Uhh....what? When I deserialize objects get created.

0 new messages