Pages

Search

Wednesday, October 8, 2008

Serialization

To provide persistence of an object similar to stroing real time data into data base, seriliazation is used.

Namespace is System.Runtime.Serialization.

The object what we are trying to persist ,its class should be serializable.

For example
Hashtable class is serializable whose object can be persisted. Where Hashtable is a predefined refernce type.


When coming to user defined class following the syntax to make class serializable.


using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
namespace name1
{
[Serializable()]
class example
{
//Class Implementation
}
}


Example Code :

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
namespace Learnings
{
[Serializable()]
class Topic
{
private string _topicName;
private string _topicDesc;
private string _topicProposedBy;
private DateTime _topicProposedDate;
public string TopicName
{
set
{
_topicName = value;
}
get
{
return _topicName;
}
}

public string TopicDescription
{
set
{
_topicDesc = value;
}
get
{
return _topicDesc;
}
}

public string TopicProposedBy
{
set
{
_topicProposedBy = value;
}
get
{
return _topicProposedBy;
}
}


public DateTime TopicProposedDate
{
set
{
_topicProposedDate = value;
}
get
{
return _topicProposedDate;
}
}


}
}

So now an object created for class Topic can be serialized and persisted.

No comments:

Post a Comment