Serializing 2 instances of the same type, in a single Class?

  • Thread starter Thread starter Pure Krome
  • Start date Start date
P

Pure Krome

How do i serialize two instances (of some object) in the
same class? I'm not sure how i would de-serialize a
class that has two or more instances of some object type,
and how the deserialize method would KNOW which
serialized instance to read.



eg.

class Foo
{
objA arraylist()
objB arraylist()
}

public GetObjectData(...)
{
objA.GetObjectInfo(..);
objB.GetObjectInfo(..);
}


how would the DE-SERIALIZED CONSTRUCTOR know which
arraylist to de-serialize?

-PK-
 
When Foo gets serialized a key value pair is created for every field. So
for foo there is two keys "objA" and "objB" with the values being the
serialization of objA and objB respectivly. Then on deserialization the
same key value pairs exist so when it finds the key "objA" it assigns objA
to the deserialization of the value and the same occurs with objB. This
would be accomplished by the following code if you did not want to use the
default serialization method:

class Foo : ISerializable
{
ArrayList objA;
ArrayList objB;

void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.Add("objA", objA);
info.Add("objB", objB);
}

public Foo(SerializationInfo info, StreamingContext context))
{
objA = info.GetValue("objA", typeof(ArrayList);
objB = info.GetValue("objB", typeof(ArrayList);
}
}

If you use the default serialization method by adding the attribute
[Serializable] to Foo I think it does pretty much the same thing.

I am not sure if this answers you question let me know if you have any
further questions.

Thanks,

Ryan Byington [MS]

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


--------------------
 
Back
Top