Object Serialization

G

Guest

I extended the class TreeNode to add some properties of my liking. Anyway,
no problems there, I can add my derived TreeNode into TreeNodeCollections and
use the properties etc. However, when I serialize a class containing my new
TreeNode, it doesn't seem to save all the properties back up the inheritance
chain. The class that is getting serialized has one member to be serialized,
a Dictionary<string, List<IOTreeNode>> where IOTreeNode is the TreeNode
derived class. The List generally has 3 to 5 IOTreeNodes in it. The extra
properties of those are fine. The problem is (totally assuming) that the
Nodes collection of the IOTreeNode is a TreeNodeCollection. So I think the
serializer is serializing TreeNodes inside the Nodes collection even though
the collection contains IOTreeNodes. I can't override the Nodes, and I can't
create a derived TreeNodeCollection.

Any pointers/ideas?

Thanks.
 
F

Frans Bouma [C# MVP]

GreyAlien007 said:
I extended the class TreeNode to add some properties of my liking.
Anyway, no problems there, I can add my derived TreeNode into
TreeNodeCollections and use the properties etc. However, when I
serialize a class containing my new TreeNode, it doesn't seem to save
all the properties back up the inheritance chain. The class that is
getting serialized has one member to be serialized, a
Dictionary<string, List<IOTreeNode>> where IOTreeNode is the TreeNode
derived class. The List generally has 3 to 5 IOTreeNodes in it. The
extra properties of those are fine. The problem is (totally
assuming) that the Nodes collection of the IOTreeNode is a
TreeNodeCollection. So I think the serializer is serializing
TreeNodes inside the Nodes collection even though the collection
contains IOTreeNodes. I can't override the Nodes, and I can't create
a derived TreeNodeCollection.

Did you implement ISerializable on your own TreeNode class? If so, is
the GetObjectData() method called when the data is serialized?
(debugger will let you know, though it's tricky to catch breakpoints
during serialization).

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
G

Guest

I did not implement ISerializable. I'm using VS2005, whose documentation
says to use the OnSerializing, OnSerialized, OnDeserializing, OnDeserialized
attributes as the preferred method to control serialization. I've used them
before, it's pretty straight forward. However, I have no idea what I should
be doing. The problem is, the serialization functions only get called once
for each root node...if they got called for every node in the nodes
collection I could probably do something useful. I've never use
ISerializable interface, does it let me do something extra special?
 
F

Frans Bouma [C# MVP]

GreyAlien007 said:
I did not implement ISerializable. I'm using VS2005, whose
documentation says to use the OnSerializing, OnSerialized,
OnDeserializing, OnDeserialized attributes as the preferred method to
control serialization. I've used them before, it's pretty straight
forward. However, I have no idea what I should be doing. The
problem is, the serialization functions only get called once for each
root node...if they got called for every node in the nodes collection
I could probably do something useful. I've never use ISerializable
interface, does it let me do something extra special?

TreeNode already implements ISerializable, so you don't have to do
that again in your class. When a binary/soap formatter serializes an
object graph, it will check if the object to serialize implenents
ISerializable. If it does, it calls GetObjectData() on that object to
get the data of the object to serialize. When the object is
deserialized from data, the deserialization constructor (TreeNode
(SerializationInfo, StreamingContext) ) is called, and as TreeNode is
already implementing ISerializable, they've created 2 methods for
inheriters of TreeNode:
Deserialize() and Serialize().
You should override those two methods.
Say I have my own class: which has some bogus membervar _someVar.
[Serializable]
public class MyTreeNode : TreeNode
{
private int _someVar;

public MyTreeNode(string text) : base(text)
{
_someVar=10;
}

public int SomeVar
{
get { return _someVar;}
set { _someVar = value;}
}
}


Now, when I serialize this class I won't get _someVar in the data and
thus when I deserialize the data, I won't get it back.

To get that done, I need to override Serialize() and Deserialize(), and
be sure the base methods are called as well:

[Serializable]
public class MyTreeNode : TreeNode
{
private int _someVar;

public MyTreeNode(string text) : base(text)
{
_someVar=10;
}

public int SomeVar
{
get { return _someVar;}
set { _someVar = value;}
}

protected override void Serialize (SerializationInfo si,
StreamingContext context)
{
// add my membervariable to the data for serialization
si.AddValue("_someVar", _someVar);
// be sure to call the base class method to get the rest
// of the data added as well.
base.Serialize(si, context);
}

protected override void Deserialize (SerializationInfo
si, StreamingContext context)
{
// read the value back
_someVar = si.GetInt32("_someVar");
// deserialize the rest of the data.
base.Deserialize(si, context);
}
}

That's basicly it. You did this and it didnt work?

Frans




--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 

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