/// <Summary>
/// Serialize the object
/// </Summary>
/// <Param name = "OBJ"> </param>
/// <Returns> </returns>
Public static string serializeobject (Object OBJ)
{
Iformatter IFOR = new binaryformatter ();
String STR = "";
Using (memorystream Sm = new memorystream ())
{
IFOR. serialize (SM, OBJ); // serialization
SM. Seek (0, seekorigin. Begin );
Byte [] bytes = new byte [Sm. Length];
Bytes = Sm. toarray ();
STR = convert. tobase64string (bytes );
STR = httpcontext. Current. server. urlencode (STR); // Encoding
}
Return STR;
}
# Endregion
# Region deserialization string is an object
/// <Summary>
/// Deserialization object
/// </Summary>
/// <Returns> </returns>
Public static object deserializeobject (string Str)
{
STR = httpcontext. Current. server. urldecode (STR); // decode
Byte [] bt = convert. frombase64string (STR );
Stream smnew = new memorystream (BT );
Iformatter fmnew = new binaryformatter ();
Object OBJ = fmnew. deserialize (smnew); // deserialization
Return OBJ;
}
# Endregion
/** // <Summary>
/// Serialize the object to obtain the base64 string
/// </Summary>
/// <Param name = "OBJ"> object to be serialized </param>
/// <Returns> </returns>
Public static string objecttobase64str (Object OBJ)
{
Return convert. tobase64string (objtobytes (OBJ ));
}
/** // <Summary>
/// Deserialize the base64 string to obtain the object
/// </Summary>
/// <Param name = "scrstr"> string to be deserialized </param>
/// <Param name = "objtype"> Object Type </param>
/// <Returns> </returns>
Public static object base64strtoobject (string scrstr, type objtype)
{
Return bytestoobj (convert. frombase64string (scrstr), objtype );
}
/** // <Summary>
/// Serialize the object to obtain the string
/// </Summary>
/// <Param name = "OBJ"> object to be serialized </param>
/// <Returns> </returns>
Public static string objecttostring (Object OBJ)
{
Return encoding. Default. getstring (objtobytes (OBJ ));
}
/** // <Summary>
/// String deserialization to get the object
/// </Summary>
/// <Param name = "scrstr"> string to be deserialized </param>
/// <Param name = "objtype"> Object Type </param>
/// <Returns> </returns>
Public static object stringtoobject (string scrstr, type objtype)
{
Return bytestoobj (encoding. Default. getbytes (scrstr), objtype );
}
/** // <Summary>
/// Serialize the object into a byte array
/// </Summary>
/// <Param name = "OBJ"> </param>
/// <Returns> </returns>
Private Static byte [] objtobytes (Object OBJ)
{
Xmlserializer SER = new xmlserializer (obj. GetType ());
Memorystream mem = new memorystream ();
Xmltextwriter writer = new xmltextwriter (MEm, encoding. Default );
Ser. serialize (writer, OBJ );
Writer. Close ();
Return mem. toarray ();
}
/** // <Summary>
/// Get the object from the byte array deserialization
/// </Summary>
/// <Param name = "objbytes"> </param>
/// <Returns> </returns>
Private Static object bytestoobj (byte [] objbytes, type objtype)
{
Try
{
Xmlserializer SER = new xmlserializer (objtype );
Memorystream mem = new memorystream (objbytes );
Streamreader sr = new streamreader (MEm, encoding. Default );
Return Ser. deserialize (SR );
}
Catch
{
Return NULL;
}
}