473,378 Members | 1,500 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,378 software developers and data experts.

How to serialize an object which refer another object?

Hi,

I have a class like the following:
class A {
private B _b;

A (B b) {
_b = b;
}
...
public B B {
get { return _b; }
}
}

and:
class B {
...
}

How to serialize an A object and B object? Maybe another serialzied
object C has
a B reference also.

Jerry
Nov 16 '05 #1
3 2362
Hi Jerry,

In the code below, notice that I used the Serializable attribute on the
types I want to serialize. Also, when I performed the serialization, I did
it on the A type, which was the root of the graph. Notice that after
serialization and deserialization that the B type that the A type references
is deserialized properly because the A type holds a reference to it.

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

[Serializable]
class A
{
private B _b;

public A (B b)
{
_b = b;
}

public B B
{
get { return _b; }
}
}

[Serializable]
class B
{
public string SomeVal;
}

class Test
{
static void Main()
{
B b = new B();
b.SomeVal = "I'm a B";
A a1 = new A(b);

MemoryStream memStr = null;

try
{
memStr = new MemoryStream();

BinaryFormatter binFmt = new BinaryFormatter();

binFmt.Serialize(memStr, a1);

memStr.Position = 0;

A a2 = (A)binFmt.Deserialize(memStr);

Console.WriteLine(a2.B.SomeVal);
}
finally
{
if (memStr != null)
{
memStr.Close();
}
}

Console.ReadLine();
}
}
Joe
--
http://www.csharp-station.com

"Jerry" <zh**********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi,

I have a class like the following:
class A {
private B _b;

A (B b) {
_b = b;
}
...
public B B {
get { return _b; }
}
}

and:
class B {
...
}

How to serialize an A object and B object? Maybe another serialzied
object C has
a B reference also.

Jerry

Nov 16 '05 #2
Hi Joe,

Thank you for your help. But I have another question. If I have two
object a1 and a2, the
B property of them is the same object b; e.g:
B b = new B();
A a1 = new A(b);
A a2 = new A(b);

If I serialize them and get object d_b1, d_a1, d_a2 by deserialize. d_a1.B
would not equal d_a2.B.
Acutally, I want get them same reference for d_a1.B and d_a2.B. How to do
it?

Jerry

"Joe Mayo" <jm***@nospamAtCSharpDashStation.com> wrote in message
news:#q**************@TK2MSFTNGP12.phx.gbl...
Hi Jerry,

In the code below, notice that I used the Serializable attribute on the
types I want to serialize. Also, when I performed the serialization, I did it on the A type, which was the root of the graph. Notice that after
serialization and deserialization that the B type that the A type references is deserialized properly because the A type holds a reference to it.

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

[Serializable]
class A
{
private B _b;

public A (B b)
{
_b = b;
}

public B B
{
get { return _b; }
}
}

[Serializable]
class B
{
public string SomeVal;
}

class Test
{
static void Main()
{
B b = new B();
b.SomeVal = "I'm a B";
A a1 = new A(b);

MemoryStream memStr = null;

try
{
memStr = new MemoryStream();

BinaryFormatter binFmt = new BinaryFormatter();

binFmt.Serialize(memStr, a1);

memStr.Position = 0;

A a2 = (A)binFmt.Deserialize(memStr);

Console.WriteLine(a2.B.SomeVal);
}
finally
{
if (memStr != null)
{
memStr.Close();
}
}

Console.ReadLine();
}
}
Joe
--
http://www.csharp-station.com

"Jerry" <zh**********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi,

I have a class like the following:
class A {
private B _b;

A (B b) {
_b = b;
}
...
public B B {
get { return _b; }
}
}

and:
class B {
...
}

How to serialize an A object and B object? Maybe another serialzied
object C has
a B reference also.

Jerry


Nov 16 '05 #3
It should work if you can serialize them both in the same graph:

binFmt.Serialize(memStr, new A[] { a1, a2 });
Console.WriteLine(Object.ReferenceEquals(a1.B, a2.B));

memStr.Position = 0;

A[] arr = (A[])binFmt.Deserialize(memStr);

Console.WriteLine(Object.ReferenceEquals(arr[0].B, arr[1].B));

Otherwise, there isn't a way to know that a1 and a2, which were serialized
separately, originally refered to the same b.

Another thought is to implement ISerializable. Make sure every object has a
unique ID (i.e. GUID) when you serialize it and create your own mechanism to
rebuild references based on object ID.

Joe
--
http://www.csharp-station.com

"Jerry" <zh**********@hotmail.com> wrote in message
news:uz**************@TK2MSFTNGP14.phx.gbl...
Hi Joe,

Thank you for your help. But I have another question. If I have two
object a1 and a2, the
B property of them is the same object b; e.g:
B b = new B();
A a1 = new A(b);
A a2 = new A(b);

If I serialize them and get object d_b1, d_a1, d_a2 by deserialize. d_a1.B
would not equal d_a2.B.
Acutally, I want get them same reference for d_a1.B and d_a2.B. How to do
it?

Jerry

"Joe Mayo" <jm***@nospamAtCSharpDashStation.com> wrote in message
news:#q**************@TK2MSFTNGP12.phx.gbl...
Hi Jerry,

In the code below, notice that I used the Serializable attribute on the
types I want to serialize. Also, when I performed the serialization, I

did
it on the A type, which was the root of the graph. Notice that after
serialization and deserialization that the B type that the A type

references
is deserialized properly because the A type holds a reference to it.

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

[Serializable]
class A
{
private B _b;

public A (B b)
{
_b = b;
}

public B B
{
get { return _b; }
}
}

[Serializable]
class B
{
public string SomeVal;
}

class Test
{
static void Main()
{
B b = new B();
b.SomeVal = "I'm a B";
A a1 = new A(b);

MemoryStream memStr = null;

try
{
memStr = new MemoryStream();

BinaryFormatter binFmt = new BinaryFormatter();

binFmt.Serialize(memStr, a1);

memStr.Position = 0;

A a2 = (A)binFmt.Deserialize(memStr);

Console.WriteLine(a2.B.SomeVal);
}
finally
{
if (memStr != null)
{
memStr.Close();
}
}

Console.ReadLine();
}
}
Joe
--
http://www.csharp-station.com

"Jerry" <zh**********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi,

I have a class like the following:
class A {
private B _b;

A (B b) {
_b = b;
}
...
public B B {
get { return _b; }
}
}

and:
class B {
...
}

How to serialize an A object and B object? Maybe another serialzied object C has
a B reference also.

Jerry



Nov 16 '05 #4

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

Similar topics

7
by: Ian Tompsett | last post by:
H I was wondering if it possible for an object to serialize/deserialize itself from XML. I'd be guessing that it would need to use the XmlSerializer class, but that seems to want to create a...
14
by: vince | last post by:
Can I add (append) to an xml file that already contains a serialized object, and be able to deserialize to either or both objects from the same file...??? How is this done...?? thanks, vince
5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
10
by: Dan | last post by:
All I Am Attempting To Serialize An Object To An XML File. Here Is The Code For That public string SaveNewSurvey( MutualSurveyObject mso_TempObject, int i_JobID ) { string s_RootFileName;...
1
by: Marcin | last post by:
hi, I would like to ask how to serialize class with members like SqlParameter. I try to use BinaryFormatter. public class Example_Class { public SqlParameter sqlParam; public...
7
by: Ben Amada | last post by:
I've created a class that I need to store in ViewState. However when I try to store it in ViewState, I get the following error: "The type 'solution.pe2' must be marked as Serializable or have a...
1
by: Tim | last post by:
Could anyone tell me what this means and how do I correct it. Any suggestions? Thanks! Tim Richardson IT Developer and Consultant www.paladin3d.com Unable to serialize the session state. In...
3
by: Matt Fielder | last post by:
I need to find a way of serializing an object to a field in an msde table. What I'm serializing is a report definition of a 3rd party report generator. It's normally stored as a file on the local...
3
by: Julie | last post by:
Here's the scenario (public attributes, etc. omitted for brevity): class Base { } class Derived : Base { }
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
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...

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.