interfaces and serializing

R

raj

SORRY FOR CROSS POST JUST WANT TO GET AN ANSWER QUICKLY

I know what interfaces are and what they used for etc. Today i am learning
about serilization.

I know to mark the class "serializable" and implement ISerializable
interface, and then implement mthod GetObjectData, etc.

I also know to instantiate a class that is marked "serializable" and use
serialize method of binaryformatter on the class and write to a stream or
whatever.

The question is how does Serialize method of binaryformatter know to call
the GetObjectData method in my class that is marked serializable.

Can someone point me into the right direction?

Specifically to the code:
how does bf.Serialize(sw, sc); know to invoke SerializeClass .GetObjectData
method???????


thanks,

raj


[Serializable()]

public class SerializeClass : ISerializable

{

private string firstName;

public string FirstName

{

get { return firstName; }

set { firstName = value; }

}

private string lastName;

public string LastName

{

get { return lastName; }

set { lastName = value; }

}

private int Age;

public int Age1

{

get { return Age; }

set { Age = value; }

}

public SerializeClass(string fName, string lName, int age)

{

firstName = fName;

lastName = lName;

Age = age;

}

public SerializeClass(SerializationInfo info, StreamingContext ctxt)

{

firstName = (string)info.GetValue("FirstName", typeof(string));

lastName = (string)info.GetValue("LastName", typeof(string));

Age = (int)info.GetValue("Age", typeof(int));

}

#region ISerializable Members

public void GetObjectData(SerializationInfo info, StreamingContext context)

{

info.AddValue("FirstName", firstName);

info.AddValue("LastName", lastName);

info.AddValue("Age", Age);

}

#endregion

}



public static void Main(string[] args)

{

SerializeClass sc = new SerializeClass("Raj", "c", 24);

Stream sw = File.Open("test1.txt", FileMode.OpenOrCreate);

BinaryFormatter bf = new BinaryFormatter();

Console.WriteLine("writing info");

bf.Serialize(sw, sc);

sw.Close();

sc = null;

sw = File.Open("test1.txt", FileMode.Open);

bf = new BinaryFormatter();

Console.WriteLine("reading from file");

sc = (SerializeClass)bf.Deserialize(sw);

sw.Close();

Console.WriteLine("First Name: {0}", sc.FirstName);

Console.WriteLine("Last Name: {0}", sc.LastName);

Console.WriteLine("Age: {0}", sc.Age1);

}
 
M

Michael Nemtsev

Hello Raj,

Man, crossposting doesn't inspur smbd to answer quickly.
These .NET groups are monitored the same peoples (in general), it doesnt
differ significatly where are u asking

R> SORRY FOR CROSS POST JUST WANT TO GET AN ANSWER QUICKLY
R>
R> I know what interfaces are and what they used for etc. Today i am
R> learning about serilization.
R>
R> I know to mark the class "serializable" and implement ISerializable
R> interface, and then implement mthod GetObjectData, etc.
R>
R> I also know to instantiate a class that is marked "serializable" and
R> use serialize method of binaryformatter on the class and write to a
R> stream or whatever.
R>
R> The question is how does Serialize method of binaryformatter know to
R> call the GetObjectData method in my class that is marked
R> serializable.
R>
R> Can someone point me into the right direction?
R>
R> Specifically to the code:
R> how does bf.Serialize(sw, sc); know to invoke SerializeClass
R> .GetObjectData
R> method???????
R> thanks,
R>
R> raj
R>
R> [Serializable()]
R>
R> public class SerializeClass : ISerializable
R>
R> {
R>
R> private string firstName;
R>
R> public string FirstName
R>
R> {
R>
R> get { return firstName; }
R>
R> set { firstName = value; }
R>
R> }
R>
R> private string lastName;
R>
R> public string LastName
R>
R> {
R>
R> get { return lastName; }
R>
R> set { lastName = value; }
R>
R> }
R>
R> private int Age;
R>
R> public int Age1
R>
R> {
R>
R> get { return Age; }
R>
R> set { Age = value; }
R>
R> }
R>
R> public SerializeClass(string fName, string lName, int age)
R>
R> {
R>
R> firstName = fName;
R>
R> lastName = lName;
R>
R> Age = age;
R>
R> }
R>
R> public SerializeClass(SerializationInfo info, StreamingContext ctxt)
R>
R> {
R>
R> firstName = (string)info.GetValue("FirstName", typeof(string));
R>
R> lastName = (string)info.GetValue("LastName", typeof(string));
R>
R> Age = (int)info.GetValue("Age", typeof(int));
R>
R> }
R>
R> #region ISerializable Members
R>
R> public void GetObjectData(SerializationInfo info, StreamingContext
R> context)
R>
R> {
R>
R> info.AddValue("FirstName", firstName);
R>
R> info.AddValue("LastName", lastName);
R>
R> info.AddValue("Age", Age);
R>
R> }
R>
R> #endregion
R>
R> }
R>
R> public static void Main(string[] args)
R>
R> {
R>
R> SerializeClass sc = new SerializeClass("Raj", "c", 24);
R>
R> Stream sw = File.Open("test1.txt", FileMode.OpenOrCreate);
R>
R> BinaryFormatter bf = new BinaryFormatter();
R>
R> Console.WriteLine("writing info");
R>
R> bf.Serialize(sw, sc);
R>
R> sw.Close();
R>
R> sc = null;
R>
R> sw = File.Open("test1.txt", FileMode.Open);
R>
R> bf = new BinaryFormatter();
R>
R> Console.WriteLine("reading from file");
R>
R> sc = (SerializeClass)bf.Deserialize(sw);
R>
R> sw.Close();
R>
R> Console.WriteLine("First Name: {0}", sc.FirstName);
R>
R> Console.WriteLine("Last Name: {0}", sc.LastName);
R>
R> Console.WriteLine("Age: {0}", sc.Age1);
R>
R> }
R>
---
WBR,
Michael Nemtsev :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
D

Dave Sexton

Hi,

The question is how does Serialize method of binaryformatter know to call
the GetObjectData method in my class that is marked serializable.

Can someone point me into the right direction?

Specifically to the code:
how does bf.Serialize(sw, sc); know to invoke SerializeClass .GetObjectData
method???????

The designers of the BinaryFormatter (or some base class) decided to check the
object passed to the Serialize method, using Reflection, to see if it is
Serializable. If it is, then the formatter checks if the object is
ISerializable. If it is, then it calls GetObjectData.

(You can bet that it's actually a bit more complicated than my basic
description.)

Rotor, Microsoft's open source CLI, may contain the BinaryFormatter's class
hierarchy and you'll be able to see exactly how it works.

Download Rotor 2.0 on MSDN:
http://www.microsoft.com/downloads/...61-3F26-4555-AE17-3121B4F51D4D&displaylang=en

A program called Reflector is also available that, like ildasm.exe only much
better, will show you meta information about any assembly, in the FCL or your
own, and it can also display a reverse engineered code snippet for any method
you'd like to see in any of several code languages as well.

Lutz Roeder's blog (author of Reflector):
http://www.aisto.com/Roeder/

Download programming tools such as Reflector:
http://www.aisto.com/Roeder/DotNet

<snip>
 

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