Serializable() and ISerializable

T

Techno_Dex

I am trying to figure out what .NET does to a class that is marked as
Serializable in order to get the class to serialize. I know I can implement
the ISerializable interface, but I don't want to hand code all of the
classes that I have marked as Serializable, the standard .NET functionality
will work just fine for me, but I do want to know when the Serialization is
occuring in my class so I can handle the access of the public properties in
a different fashion. Can anyone explain to me what I need to put in the
ISerializable.GetObjectData() method to tell .NET to serialize as it
normally would?

[Serializable{}]
public partial class Test : ISerializable
{
private int List<CustomObject> mlCustomObjects = null;
private bool mbSerializingNow = false;

public List<CustomObject> CustomObjects
{
get
{
//I only want to instantiate the mlCustomObject List if it
is being requested by code, but not during the Serialization process.
if ((mlCustomObjects == null) && (mbSerializingNow ==
false))
{
mlCustomObjects = new List<CustomObject>();
}
return mlCustomObjects;
}
set { mlCustomObjects = value; }
}

public void GetObjectData (SerializationInfo info, StreamingContext
context)
{
mbSerializingNow = true;
//******************************************
// What do I do in here to get .NET to perform its standard
serialization???
//******************************************
mbSerializingNow = false;
}
}
 
G

Guest

The example in the documentation for GetDataObject [1] is fairly
straightforward. While it's largely implementation specific, basically use
SerializationInfo.AddValue() to serialize the values of value types. For
reference types, if they are serializable you can simply call their
GetDataObject method--otherwise you'll have to manually serialize all the
members needed to deserialize that object (again, with AddValue).

Implementing GetDataObject is one half of a symmetrical set of actions. To
deserialize you need to implement the serializing constructor
(ClassName(SerializationInfo info, StreamingContext context)) in which you
call, in the same order, the inverse SerializationInfo methods like GetValue,
Get{TypeName}, or an aggregated reference type's serializing constructor
(when a reference type implements ISerializable, otherwise you'll have to
manually create the object and populate its properties). Simon Lok has an
example of chaining calls to GetDataObject/Serializing-constructor for
ISerializable types here:
http://www1.cs.columbia.edu/~lok/cs...untime.Serialization/types/ISerializable.html

When using the SerializableAttribute, the CLR reflects upon the type to
automatically serialize properties not attributed with
NotSerializableAttribute. A drawback of using the SerializableAttribute is
versioning complexities. You have to carefully attribute your properties to
handle versioning. See
http://msdn2.microsoft.com/en-us/library/ms229752.aspx for some details.

[1]
http://msdn2.microsoft.com/en-us/li...erialization.iserializable.getobjectdata.aspx
--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#


Techno_Dex said:
I am trying to figure out what .NET does to a class that is marked as
Serializable in order to get the class to serialize. I know I can implement
the ISerializable interface, but I don't want to hand code all of the
classes that I have marked as Serializable, the standard .NET functionality
will work just fine for me, but I do want to know when the Serialization is
occuring in my class so I can handle the access of the public properties in
a different fashion. Can anyone explain to me what I need to put in the
ISerializable.GetObjectData() method to tell .NET to serialize as it
normally would?

[Serializable{}]
public partial class Test : ISerializable
{
private int List<CustomObject> mlCustomObjects = null;
private bool mbSerializingNow = false;

public List<CustomObject> CustomObjects
{
get
{
//I only want to instantiate the mlCustomObject List if it
is being requested by code, but not during the Serialization process.
if ((mlCustomObjects == null) && (mbSerializingNow ==
false))
{
mlCustomObjects = new List<CustomObject>();
}
return mlCustomObjects;
}
set { mlCustomObjects = value; }
}

public void GetObjectData (SerializationInfo info, StreamingContext
context)
{
mbSerializingNow = true;
//******************************************
// What do I do in here to get .NET to perform its standard
serialization???
//******************************************
mbSerializingNow = false;
}
}
 
T

Techno_Dex

Peter, thanks for the reply. My hope was more so to access the underlying
..NET Framework functionality that performs the serialization on my behalf
(presumable through reflection, etc). I don't want to hand code all of the
serialization/deserialization for all of my classes as I'm not doing
anything specific that would require the hand coding, rather I wanted to
simulate an event which would indicate that serialization was occuring. I
should have known better when implementing the interface ISerializable,
since interfaces don't actually have any code to them. I guess I wil
investigate another path for development.

Peter Ritchie said:
The example in the documentation for GetDataObject [1] is fairly
straightforward. While it's largely implementation specific, basically
use
SerializationInfo.AddValue() to serialize the values of value types. For
reference types, if they are serializable you can simply call their
GetDataObject method--otherwise you'll have to manually serialize all the
members needed to deserialize that object (again, with AddValue).

Implementing GetDataObject is one half of a symmetrical set of actions.
To
deserialize you need to implement the serializing constructor
(ClassName(SerializationInfo info, StreamingContext context)) in which you
call, in the same order, the inverse SerializationInfo methods like
GetValue,
Get{TypeName}, or an aggregated reference type's serializing constructor
(when a reference type implements ISerializable, otherwise you'll have to
manually create the object and populate its properties). Simon Lok has an
example of chaining calls to GetDataObject/Serializing-constructor for
ISerializable types here:
http://www1.cs.columbia.edu/~lok/cs...untime.Serialization/types/ISerializable.html

When using the SerializableAttribute, the CLR reflects upon the type to
automatically serialize properties not attributed with
NotSerializableAttribute. A drawback of using the SerializableAttribute
is
versioning complexities. You have to carefully attribute your properties
to
handle versioning. See
http://msdn2.microsoft.com/en-us/library/ms229752.aspx for some details.

[1]
http://msdn2.microsoft.com/en-us/li...erialization.iserializable.getobjectdata.aspx
--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#


Techno_Dex said:
I am trying to figure out what .NET does to a class that is marked as
Serializable in order to get the class to serialize. I know I can
implement
the ISerializable interface, but I don't want to hand code all of the
classes that I have marked as Serializable, the standard .NET
functionality
will work just fine for me, but I do want to know when the Serialization
is
occuring in my class so I can handle the access of the public properties
in
a different fashion. Can anyone explain to me what I need to put in the
ISerializable.GetObjectData() method to tell .NET to serialize as it
normally would?

[Serializable{}]
public partial class Test : ISerializable
{
private int List<CustomObject> mlCustomObjects = null;
private bool mbSerializingNow = false;

public List<CustomObject> CustomObjects
{
get
{
//I only want to instantiate the mlCustomObject List if
it
is being requested by code, but not during the Serialization process.
if ((mlCustomObjects == null) && (mbSerializingNow ==
false))
{
mlCustomObjects = new List<CustomObject>();
}
return mlCustomObjects;
}
set { mlCustomObjects = value; }
}

public void GetObjectData (SerializationInfo info, StreamingContext
context)
{
mbSerializingNow = true;
//******************************************
// What do I do in here to get .NET to perform its standard
serialization???
//******************************************
mbSerializingNow = false;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top