Serialization and deserialization Methods

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.