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
}