473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Object Serialization

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.
May 24 '06 #1
3 6647
GreyAlien007 wrote:
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#)
------------------------------------------------------------------------
May 24 '06 #2
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?

"Frans Bouma [C# MVP]" wrote:
GreyAlien007 wrote:
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#)
------------------------------------------------------------------------

May 24 '06 #3
GreyAlien007 wrote:
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

"Frans Bouma [C# MVP]" wrote:
GreyAlien007 wrote:
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).

--
------------------------------------------------------------------------
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#)
------------------------------------------------------------------------
May 25 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: NewToDotNet | last post by:
I am getting "Object reference not set to an instance of an object. " when I attempt to open a C# windows service class in design view, although I was able to initially create the service and open...
0
by: Philip Reed | last post by:
I'm trying to write a preferences-handling infrastructure that serializes prefs to XML. Basically I want to read in a common "default" prefs set, then read in the user's prefs and override the...
5
by: Mark Rae | last post by:
Hi, Can anyone please tell me how to convert an object say, a System.Web.Mail.MailMessage object, to a byte array and then convert the byte array to a Base64 string? Any assistance gratefully...
8
by: rawCoder | last post by:
Hi All, I need some advanced samples or references for passing custom objects over the network using sockets. Without using Remoting what are other options in .NET Framework for this binary...
3
by: AVL | last post by:
Hi, I'm new to .net. I need some info on serialization. What is serialization? Why do we need it? Why objects need to be serialized if they need to be stored in session or viewstate?
5
by: Matthew | last post by:
I have a nice little Sub that saves data in a class "mySettings" to an XML file. I call it like so: Dim mySettings As mySettings = New mySettings mySettings.value1 = "someText" mySettings.value2...
1
by: J. Askey | last post by:
I am implementing a web service and thought it may be a good idea to return a more complex class (which I have called 'ServiceResponse') in order to wrap the original return value along with two...
3
by: benkial | last post by:
Below is a custom exception class that I created to be shared by my C+ + and C# code. It works fine till I need to pass the exception object through Remoting: every time a FtException is raized in...
0
by: anchiang | last post by:
Hi All, I have XML: <RegistryResponse status="Success" xmlns="urn:oasis:names:tc:ebxml-regrep:registry:xsd:2.1"> <AdhocQueryResponse xmlns="urn:oasis:names:tc:ebxml-regrep:query:xsd:2.1"> ...
3
by: Jeremy | last post by:
I've created a serializable class and put attributes around all the properties that should be serialized. I return the class from a web service, but my problem is that the wsdl for the web service...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.