Can the delegate be serialized?

Source: Internet
Author: User

 

 

XmlSerialize: Serialization is the process of converting an object into a form that is easy to transmit. For example, an object can be serialized and transmitted between the client and server over the Internet through HTTP. On the other hand, deserialization reconstructs objects in the stream.

XML serialization only serializes public fields and attribute values of objects into XML streams. XML serialization does not include type information. For example, ifLibraryNamespace existsBookObject, it cannot be guaranteed to be deserialized into an object of the same type.

Note::

XML serialization cannot convert methods, indexers, private fields, or read-only attributes (except for read-only sets ). To serialize all public and private fields and attributes of an object, use BinaryFormatter instead of XML serialization.

 SoapFormaterr:

Serialize and deserialize the image of an object or the entire connected object in SOAP format.

BinaryFormatter:

Serialize and deserialize an object or the entire connected object in binary format.

You can use BinaryFormatter and SoapFormatter to delegateSerialization, but cannot pass xmlSerializeSerialization.

Using System;

 

Using System. Runtime. Serialization. Formatters. Soap;

Using System. IO;

Using System. Runtime. Serialization. Formatters. Binary;

Using System. Xml;

Using System. Xml. Serialization;

 

Class Program

{

Delegate void foo (string formatter );

 

Static void Main (string [] args)

{

 

Foo TestHandler = new foo (Test );

 

BinaryFormatter bFormatter = new BinaryFormatter ();

 

 

SoapFormatter sFormatter = new SoapFormatter ();

 

 

Try

{

XmlSerializer xFormatter = new XmlSerializer (typeof (foo ));

}

Catch (Exception ex)

{

Console. WriteLine (ex. Message );

}

 

TestHandler ("None ");

 

String filePath = AppDomain. CurrentDomain. BaseDirectory;

String fileName = "soap. xml ";

String fullPath = Path. Combine (filePath, fileName );

 

String fileName2 = "binary.txt ";

String fullPath2 = Path. Combine (filePath, fileName2 );

 

// String fileName3 = "xml. xml ";

// String fullPath3 = Path. Combine (filePath, fileName3 );

Using (FileStream fs = new FileStream (fullPath, FileMode. Create ))

{

SFormatter. Serialize (fs, TestHandler );

Fs. Close ();

 

 

}

 

Using (FileStream fs2 = new FileStream (fullPath2, FileMode. Create ))

{

BFormatter. Serialize (fs2, TestHandler );

Fs2.Close ();

}

 

Using (FileStream fs = new FileStream (fullPath, FileMode. Open ))

{

Foo deserHandler = (foo) sFormatter. Deserialize (fs );

DeserHandler ("Soap ");

Fs. Close ();

}

 

Using (FileStream fs = new FileStream (fullPath2, FileMode. Open ))

{

Foo deserHandler = (foo) bFormatter. Deserialize (fs );

DeserHandler ("Binarry ");

Fs. Close ();

}

 

Console. ReadLine ();

}

 

Static void Test (string formatter)

{

Console. writeLine ("Formatter: {2} Test invoked. call Time: {0} ticks: {1} ", DateTime. now. toString (), DateTime. now. ticks. toString (), formatter );

}

}

Running result:

 

 

 

 

 

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.