473,416 Members | 1,836 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,416 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 2365
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 { }
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.