[★]. Net serialization in binary format

Source: Internet
Author: User
Tags net serialization

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. runtime. serialization;
Using system. runtime. serialization. formatters. Binary;
Using system. IO;
Namespace csharptest
{
/// <Summary>
///
/// Serialization advantages
/// Make the custom object persistent in some way;
/// The object can be easily passed to another place
///
/// The essence of serialization is to convert the class value into a general (that is, continuous) byte stream, the stream can then be written to a disk file or any other target that can be strean.
/// To actually write this stream, use the serialize and deserialize methods that implement the iformatter interface.
///
/// The. NET Framework provides two classes: binaryformatter and soapformatter.
/// Binaryformatter class uses binary format for serialization, while soapformatter class uses XML format for serialization.
///
/// To use the binaryformater class, you only need to create an instance of the stream to be used and an instance of the serialization class.
/// The stream instance and the object instance to be serialized are provided to call this method as parameters. All member variables (including variables marked as private) in the class will be serialized.
/// Note that the serializable class must be added with the [serializable ()] flag before the class declaration.
///

/// </Summary>

// Namespace csharptest
//{
// [Serializable ()]
// Class class2
//{

// Private string _ username = string. empty;
// Public String Username
//{
// Get
//{
// Return _ username;
//}
// Set
//{
// _ Username = value;
//}
//}
// Public String kk ()
//{
// Return! ";
//}
//}
//}
// Private void button3_click (Object sender, eventargs E)
//{
// Class2 CLS = new class2 ();
// Cls. Username = "my negative value ";

// Serializationhelper. serialize (CLS, "F: \ 1.txt ");
// MessageBox. Show ("successful ");

//}

// Private void button4_click (Object sender, eventargs E)
//{
// Class2 CLS = (class2) serializationhelper. deserialize ("F: \ 1.txt ");

// MessageBox. Show (CLS. username. tostring ());
// MessageBox. Show (CLS. kk (). tostring ());
//}
Public class serializationhelper
{
// Static method serialize, serializing an object into a file
Public static void serialize (Object Data, string filepath)
{Try
{
// Open the file
Streamwriter FS = new streamwriter (filepath, false );
Try
{
// Create a stream in which the supported storage zone is memory
Memorystream streammemory = new memorystream ();
// Serialize or deserialize an object or the entire connected object in binary format
Binaryformatter formater = new binaryformatter ();
// Serialize this object to the memory stream
Formater. serialize (streammemory, data );
// Convert to a string first
String binarydata = convert. tobase64string (streammemory. getbuffer ());

// Write data to a file
FS. Write (binarydata );
}
Catch (exception ex)
{
Throw ex;
}
Finally
{
FS. Flush ();
FS. Close ();
}
}
Catch (exception ex)
{
Throw ex;
}
}
// Deserialization: serializes an object from a file
Public static object deserialize (string filepath)
{
Object Data = new object ();
Try
{
// Open the file
Streamreader sr = new streamreader (filepath );
Try
{
Memorystream streammemory;
Binaryformatter formatter = new binaryformatter ();
// Read data as a string
String cipherdata = Sr. readtoend ();

Byte [] binarydata = convert. frombase64string (cipherdata );
// Deserialization as an object
Streammemory = new memorystream (binarydata );
Data = formatter. deserialize (streammemory );
}
Catch
{
// The data cannot be obtained. Set it to null.
Data = NULL;
}
Finally
{
// Close the object
Sr. Close ();
}

}
Catch
{
// The data cannot be obtained. Set it to null.
Data = NULL;
}
// Return data
Return data;
}

}
}

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.