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);
}
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);
}