string Serialize(object ObjectData)
{
MemoryStream stream = new MemoryStream();
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(stream, ObjectData);
byte[] bArray;
bArray = stream.ToArray();
stream.Position = 0;
string tempobjectdatastring = Convert.ToBase64String(bArray);
stream.Close();
return tempobjectdatastring;
}
object DeSerialize(string datastring)
{
byte[] bArray;
bArray = Convert.FromBase64String(datastring);
MemoryStream stream = new MemoryStream();
stream.Position = 0;
stream.Write(bArray, 0, bArray.Length);
stream.Position = 0;
BinaryFormatter bformatter = new BinaryFormatter();
object tempobjectdata = bformatter.Deserialize(stream);
stream.Close();
return tempobjectdata;
}
Thursday, April 12, 2007
Easiest way to Serialize/Deserialize in C#
Labels:
Technical
Easiest way to Clone in C#
public object Clone()
{
MemoryStream stream = new MemoryStream();
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(stream, this);
stream.Position = 0;
return bformatter.Deserialize(stream);
}
{
MemoryStream stream = new MemoryStream();
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(stream, this);
stream.Position = 0;
return bformatter.Deserialize(stream);
}
Labels:
Technical
Saturday, April 7, 2007
Load any class dynamically from DLL
If you wanna load any class or create an instance of any class dynamically from any Dll
You may use "Activator"
System.Activator(,)
Use Unwrap() to typecast into specific class...
You may use "Activator"
System.Activator(
Use Unwrap() to typecast into specific class...
Labels:
Technical
Wanna Save something for request life cycle...
Do you want to save something for request life cycle?
ASP.NET 2.0 provide you a solution to save something for the request life cycle.
And you can access it from anywhere.
with httpContext.Current.Items["Key"]
In the previous blog I have showed that how to use control state,
you can save the page state similarly.
When you load it just put it with specified keys,
then any one can access it for that postback cycle,
And this will load before anything so don't worry...
ASP.NET 2.0 provide you a solution to save something for the request life cycle.
And you can access it from anywhere.
with httpContext.Current.Items["Key"]
In the previous blog I have showed that how to use control state,
you can save the page state similarly.
When you load it just put it with specified keys,
then any one can access it for that postback cycle,
And this will load before anything so don't worry...
Labels:
Technical
Difficulties With ViewState(Solve . . .)
Are you facing some difficulties with ViewState ?
ASP.NET 2.0 provide you a solution for managing ViewState
Which is called Control State
You just put your private variable in control state and write the code for which variable you want to save and load, ASP.NET will load and save them for you... Wow!
And to do that you have to override write the following code :
1. override void OnInit(EventArgs e)
2. override object SaveControlState()
3. override void LoadControlState(object state)
Typically "override void OnInit(EventArgs e)" method have the following code :
override void OnInit(EventArgs e)
{
Page.RegisterRequiresControlState(this);
base.OnInit(e);
}
And I suggest a data structure for the next two one:
Use a dictionary with Dictionary
use your variable name a key and your variable as object
Like the following code:
override object SaveControlState()
{
Pair newState = new Pair();
newState.First = base.SaveControlState();
Dictionary privateMemebrs = new Dictionary();
privateMemebrs["a"] = this.a;
privateMemebrs["b"] = this.b;
privateMemebrs["c"] = this.c;
newState.Second = privateMemebrs;
return newState;
}
protected override void LoadControlState(object state)
{
if (state == null)
return;
Pair newState = (Pair)state;
base.LoadControlState(newState.First);
if (newState.Second != null)
{
Dictionary privateMemebrs = (Dictionary)newState.Second;
this.a = (List)privateMemebrs["a"];
this.b = (int)privateMemebrs["b"];
this.c = (string)privateMemebrs["c"];
}
}
ASP.NET 2.0 provide you a solution for managing ViewState
Which is called Control State
You just put your private variable in control state and write the code for which variable you want to save and load, ASP.NET will load and save them for you... Wow!
And to do that you have to override write the following code :
1. override void OnInit(EventArgs e)
2. override object SaveControlState()
3. override void LoadControlState(object state)
Typically "override void OnInit(EventArgs e)" method have the following code :
override void OnInit(EventArgs e)
{
Page.RegisterRequiresControlState(this);
base.OnInit(e);
}
And I suggest a data structure for the next two one:
Use a dictionary with Dictionary
use your variable name a key and your variable as object
Like the following code:
override object SaveControlState()
{
Pair newState = new Pair();
newState.First = base.SaveControlState();
Dictionary
privateMemebrs["a"] = this.a;
privateMemebrs["b"] = this.b;
privateMemebrs["c"] = this.c;
newState.Second = privateMemebrs;
return newState;
}
protected override void LoadControlState(object state)
{
if (state == null)
return;
Pair newState = (Pair)state;
base.LoadControlState(newState.First);
if (newState.Second != null)
{
Dictionary
this.a = (List
this.b = (int)privateMemebrs["b"];
this.c = (string)privateMemebrs["c"];
}
}
Labels:
Technical
Subscribe to:
Posts (Atom)