Serialization and De-serialization
Serialization is the process of writing objects to the stream while De-serialization is the process of reading objects
from the stream. Up until now, we have seen how to read/write primitive types to the streams but we havent
read/written any explicit (user defined) type to the stream. There are certain points that must be clear before
actually implementing the serialization.
The purpose of serializing or writing an object to a stream is to save its state. The state of an object is determined
by its instance variables. Hence serializing an object means writing all of its member (or instance) variables (also
called an object graph) to the stream. Methods or static members are not serialized or written to the stream.
You can serialize an object yourself by simply writing all of its member variables to the stream. When
de-serializing, you would have to read all the member variables in the same sequence in which they were written.
However this process of serializing and de-serializing has two major disadvantages:
• It is a tedious job to write all the member variables yourself and it might become hectic if your class
contains a lot of variables and if your class contains other user defined objects.
• It is not the standard procedure. The person who is willing to de-serialize the object you serialized
previously, would have to be aware of the sequence in which you wrote the member variables and must
follow that sequence.
The Dot (.Net) framework takes care of these issues and provides binary and SOAP formatters using which you
can serialize your object just by calling their Serialize() and Deserialize() methods.
There is a serious security issue connected with serialization. You might not want certain classes to be serialized to
the stream or you might not want to serialize all of your member variables to the stream. For example, a
web-application may not allow the UserInfo object to be serialized or its Password field to be serialized.
All the classes in .Net are un-serializable by default - that is they can not be written to a stream. You have to
explicitly mark your class to be serializable using the [Serializable] attribute.
You can optionally mark a particular member variable (or field) as Non-Serialized by using the [NonSerialized]
attribute to prevent the CLR from writing that field when serializing the object.
Implementing Serialization and Deserialization A simple example
using System;
using System.IO; // for FileStream
using System.Runtime.Serialization.Formatters.Binary; // for BinaryFormatter
namespace Compiler
{
class Test
{
static void Main()
{
Addition addition = new Addition(3, 4);
FileStream fs = new FileStream(@"C:\C-Sharp.txt", FileMode.Create);
BinaryFormatter binForm = new BinaryFormatter();
Console.WriteLine("Serializing the object....");
binForm.Serialize(fs, addition);
fs.Position = 0; // move to the start of file
Console.WriteLine("DeSerializing the object....");
Addition sum = (Addition) binForm.Deserialize(fs);
int res = sum.Add();
Console.WriteLine("The sum of 3 and 4 is: {0}", res);
}
}
[Serializable]
class Addition
{
private int num1;
private int num2;
private int result;
public Addition()
{
}
public Addition(int num1, int num2)
{
this.num1 = num1;
this.num2 = num2;
}
public int Add()
{
result = num1 + num2;
return result;
}
Untitled Page
!doctype>
Monday, June 13, 2011
Reflecting an Assembly (Loading DLL using reflection at runtime in C#)
Reflection is the ability to find out information about objects, the application details (assemblies), its metadata at run-time.
Assembly a = Assembly.LoadFile(@"c:\Business.dll");
Response.Write("Location **** "+a.CodeBase);
Response.Write("
EntryPoint **** " + a.EntryPoint);
Response.Write("
FullName **** " + a.FullName);
Type[] types = a.GetTypes();
foreach (Type t in types)
{
Response.Write("
Type is {0} " + t);
}
Assembly a = Assembly.LoadFile(@"c:\Business.dll");
Response.Write("Location **** "+a.CodeBase);
Response.Write("
EntryPoint **** " + a.EntryPoint);
Response.Write("
FullName **** " + a.FullName);
Type[] types = a.GetTypes();
foreach (Type t in types)
{
Response.Write("
Type is {0} " + t);
}
Subscribe to:
Posts (Atom)