Serializablehelper serialization and deserialization tools

Source: Internet
Author: User

Using system;
Using system. IO;
Using system. reflection;
Using system. runtime. serialization;
Using system. runtime. serialization. formatters. Binary;
Using system. runtime. serialization. formatters. Soap;
Using system. text;
Using system. xml;
Using system. xml. serialization;

/// <Summary>

/// Serializablehelper
/// </Summary>
Public static class serializablehelper
{

# Region soap serialization-serialization and transmission of objects in XML format, completely retaining object data and status.
/// <Summary>
/// Soap serialization
/// </Summary>
/// <Param name = "OBJ"> Object </param>
/// <Returns> </returns>
Public static string soapserializable (Object OBJ)
{
String rsult = string. empty;
Try
{
Iformatter formatter = new soapformatter ();
Stream MS = new memorystream ();
Formatter. serialize (MS, OBJ );
Byte [] B = new byte [Ms. Length];
Ms. Position = 0;
Ms. Read (B, 0, B. Length );
Rsult = convert. tobase64string (B );
Ms. Close ();
Ms. Dispose ();
}
Catch (exception ex)
{
Throw ex;
}
Return rsult;
}

/// <Summary>
/// Soap deserialization
/// </Summary>
/// <Param name = "str"> serialized characters </param>
/// <Returns> </returns>
Public static t soapdeserialize <t> (string Str) where T: Class
{
T result = NULL;
Try
{
Iformatter formatter = new soapformatter ();
Byte [] B = convert. frombase64string (STR );
Stream MS = new memorystream (B );
Result = formatter. deserialize (MS) as t;
Ms. Close ();
Ms. Dispose ();
}
Catch (exception)
{

Return NULL;
}
Return result;
}

# Endregion

# Region binary serialization
/// <Summary>
/// Binary serialization
/// </Summary>
/// <Param name = "OBJ"> Object </param>
/// <Returns> </returns>
Public static string binaryserialize (Object OBJ)
{
String result = default (string );
If (OBJ! = NULL)
{
Try
{
Binaryformatter formatter = new binaryformatter ();
Memorystream stream = new memorystream ();
Formatter. serialize (stream, OBJ );
Result = encoding. Unicode. getstring (stream. getbuffer ());
}
Catch (exception ex)
{
Throw ex;
}
}
Return result;

}

/// <Summary>
/// Binary serialized object
/// </Summary>
/// <Param name = "str"> Object </param>
/// <Returns> </returns>
Public static t binarydeserialize <t> (string Str) where T: Class
{
T result = NULL;
If (Str. length> 0)
{
Try
{
Binaryformatter formatter = new binaryformatter ();
Memorystream stream = new memorystream (encoding. Unicode. getbytes (STR ));
Stream. Position = 0;
Stream. Seek (0, system. Io. seekorigin. Begin );
Result = formatter. deserialize (Stream) as t;
}
Catch (exception ex)
{
Throw ex;
}
}
Return result;
}

# Endregion

# Region WCF serialization
/// <Summary>
/// WCF serialization
/// </Summary>
/// <Param name = "OBJ"> WCF object </param>
/// <Returns> </returns>
Public static string datacontractserialize (Object OBJ)
{
String result = default (string );
If (OBJ! = NULL)
{
Try
{
Memorystream stream = new memorystream ();
System. runtime. serialization. datacontractserializer Se = new datacontractserializer (obj. GetType ());
Se. writeobject (stream, OBJ );
Stream. Position = 0;
Streamreader sr = new streamreader (Stream );

Result = Sr. readtoend ();

}
Catch (exception ex)
{
Throw ex;
}
}
Return result;
}
/// <Summary>
///
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
/// <Param name = "str"> </param>
/// <Returns> </returns>
Public static t datacontractdeserialize <t> (string Str) where T: Class
{
T result = default (t );
If (Str. length> 0)
{
Try
{
Memorystream stream = new memorystream (encoding. utf8.getbytes (STR ));
Stream. Position = 0;
Stream. Seek (0, system. Io. seekorigin. Begin );

Datacontractserializer SER = new datacontractserializer (typeof (t ));
Result = (t) SER. readobject (Stream );
Stream. Close ();
}
Catch (exception ex)
{
Throw ex;
}
}
Return result;
}

/// <Summary>
/// WCF
/// </Summary>
/// <Param name = "OBJ"> </param>
/// <Returns> </returns>
Public static string netdatacontractserialize (Object OBJ)
{
String result = default (string );
If (OBJ! = NULL)
{
Try
{
Memorystream stream = new memorystream ();
System. runtime. serialization. netdatacontractserializer Se = new netdatacontractserializer ();
Se. writeobject (stream, OBJ );
Stream. Position = 0;
Streamreader sr = new streamreader (Stream );

Result = Sr. readtoend ();

}
Catch (exception ex)
{
Throw ex;
}
}
Return result;
}
/// <Summary>
///
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
/// <Param name = "str"> </param>
/// <Returns> </returns>
Public static t netdatacontractdeserialize <t> (string Str) where T: Class
{
T result = default (t );
If (Str. length> 0)
{
Try
{
Memorystream stream = new memorystream (encoding. utf8.getbytes (STR ));
Stream. Position = 0;
Stream. Seek (0, system. Io. seekorigin. Begin );

Netdatacontractserializer SER = new netdatacontractserializer ();
Result = (t) SER. readobject (Stream );
Stream. Close ();
}
Catch (exception ex)
{
Throw ex;
}
}
Return result;
}
# Endregion

# Region XML serialization-objects are serialized and transmitted in XML format, and object data and status cannot be completely retained.
/// <Summary>
/// XML serialization
/// </Summary>
/// <Param name = "OBJ"> Object </param>
/// <Returns> </returns>
Public static string xmlserialize (Object OBJ)
{
Streamwriter Sw = NULL;
String serializestring = NULL;

Try
{
//
Xmlserializer = new xmlserializer (obj. GetType ());
Memorystream memstream = new memorystream ();
Sw = new streamwriter (memstream );
Xmlserializer. serialize (SW, OBJ );
Memstream. Position = 0;
Serializestring = encoding. utf8.getstring (memstream. getbuffer ());
}
Catch (exception ex)
{
Throw ex;
}
Finally
{
If (SW! = NULL)
{
Sw. Close ();
}
}

Return serializestring;
}

/// <Summary>
///
/// </Summary>
/// <Param name = "type"> </param>
/// <Param name = "serializedstring"> </param>
/// <Returns> </returns>
Public static t xmldeserialize <t> (string serializedstring) where T: Class
{

//
If (serializedstring. Trim (). Equals (string. Empty ))
{
Throw new exception ("the object is empty ");
}

Try
{
Xmlserializer = new xmlserializer (typeof (t ));
Stringreader = new stringreader (serializedstring );
T OBJ = xmlserializer. deserialize (stringreader) as t;

Return OBJ;
}
Catch (exception ex)
{
Throw ex;
}
}
# Endregion
}

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.