Question about Interfaces and serialization - really simple!?!

R

raj

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,

It's a work of Formater that u use. Before serializing to sream formater
analyzes your object and checks several infos that include whether the object
marked with [Serializable] attribute - in this case it checks that ISerializable
is implemented and calls the GetObjectData

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 Nietzsch
 
R

raj

One more question, am i able to add functionality of that sort in my
programs? i guess i would be using reflections, etc. correct?

thanks

raj

PS


Sorry about the crossposting on the last one!!



Michael Nemtsev said:
Hello Raj,

It's a work of Formater that u use. Before serializing to sream formater
analyzes your object and checks several infos that include whether the
object marked with [Serializable] attribute - in this case it checks that
ISerializable is implemented and calls the GetObjectData

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
 
M

Michael Nemtsev

Hello Raj,

you can, but not with reflection

There are 2 ways to custom your serialization
1) via construstor and methods
deserializing: realize private constructor with the signature (SerializationInfo
info, StreamingContext ctx) where u restore your info from the SerializationInfo
class (where your object persists)
implement void ISerializable.GetObjectData(SerializationInfo info, StreamingContext
ctx) where save your data to the info object

2) via attributes
[OnSerializing/ed], [OnDeserializing/ed]
just methods with the such attributes where save your objects

See MSDN for samples



R> One more question, am i able to add functionality of that sort in my
R> programs? i guess i would be using reflections, etc. correct?
R>
R> thanks
R>
R> raj
R>
R> PS
R>
R> Sorry about the crossposting on the last one!!
R>
R> R>
Hello Raj,

It's a work of Formater that u use. Before serializing to sream
formater analyzes your object and checks several infos that include
whether the object marked with [Serializable] attribute - in this
case it checks that ISerializable is implemented and calls the
GetObjectData

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
---
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
 
R

raj

Ah i seee...thank you so much i will look in to them...i guess i should read
up on attributes :)


Michael Nemtsev said:
Hello Raj,

you can, but not with reflection

There are 2 ways to custom your serialization
1) via construstor and methods
deserializing: realize private constructor with the signature
(SerializationInfo info, StreamingContext ctx) where u restore your info
from the SerializationInfo class (where your object persists)
implement void ISerializable.GetObjectData(SerializationInfo info,
StreamingContext ctx) where save your data to the info object

2) via attributes
[OnSerializing/ed], ?[OnDeserializing/ed]
just methods with the such attributes where save your objects

See MSDN for samples



R> One more question, am i able to add functionality of that sort in my
R> programs? i guess i would be using reflections, etc. correct?
R> R> thanks
R> R> raj
R> R> PS
R> R> Sorry about the crossposting on the last one!!
R> R>
Hello Raj,

It's a work of Formater that u use. Before serializing to sream
formater analyzes your object and checks several infos that include
whether the object marked with [Serializable] attribute - in this
case it checks that ISerializable is implemented and calls the
GetObjectData

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
---
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
 
M

Michael Nemtsev

Hello Raj,

You'd better read Troelsen book "Pro C# and .NET 2.0"
he describes aspect of custom serialization pretty well

R> Ah i seee...thank you so much i will look in to them...i guess i
R> should read up on attributes :)
R>
R> R>
Hello Raj,

you can, but not with reflection

There are 2 ways to custom your serialization
1) via construstor and methods
deserializing: realize private constructor with the signature
(SerializationInfo info, StreamingContext ctx) where u restore your
info
from the SerializationInfo class (where your object persists)
implement void ISerializable.GetObjectData(SerializationInfo info,
StreamingContext ctx) where save your data to the info object
2) via attributes
[OnSerializing/ed], ?[OnDeserializing/ed]
just methods with the such attributes where save your objects
See MSDN for samples

R> One more question, am i able to add functionality of that sort in
my
R> programs? i guess i would be using reflections, etc. correct?
R> R> thanks
R> R> raj
R> R> PS
R> R> Sorry about the crossposting on the last one!!
R> R>
Hello Raj,

It's a work of Formater that u use. Before serializing to sream
formater analyzes your object and checks several infos that include
whether the object marked with [Serializable] attribute - in this
case it checks that ISerializable is implemented and calls the
GetObjectData

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
---
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
---
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
 

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