C # custom serialization data

Source: Internet
Author: User

Because the serialized data volume using the implied parameter is implemented using the reflection technique. Therefore, the speed is somewhat problematic. The speed is improved.

Serialize uses custom serialization. To implement custom serialization, You need to implement the iserializable interface.

The token generation is as follows:

View code

 [Serializable]  Public   Class  Myobject: iserializable {  Public   Int  N1;  Public   Int  N2;  Public  String STR; Public  Myobject (){}  Protected  Myobject (serializationinfo info, streamingcontext context) {N1 = Info. getint32 ( "  I  "  ); N2 = Info. getint32 ( "  J  "  ); Str = Info. getstring ( "  K "  );} [Securitypermissionattribute (securityaction. Demand, serializationformatter = True  )]  Public   Virtual   Void  Getobjectdata (serializationinfo info, streamingcontext context) {info. addvalue (  "  I  "  , N1); Info. addvalue (  "  J "  , N2); Info. addvalue (  "  K  "  , STR );}} 

1. You need to implement this interface.

This interface method is used to determine the number of data to be serialized.

2. A structure function is required, which is the same as the structure function.

ProtectedMyobject (serializationinfo info, streamingcontext context) {N1= Info. getint32 ("I"); N2= Info. getint32 ("J"); Str= Info. getstring ("K");}

This constructor is used when the class is deserialized. Here, you can specify the content to be deserialized.

3. The runtime is the same as normal serialization.

4. There should be accepted types. When serialization is realized, an interface and a structure function should also be implemented. In the two methods

A base-type method is required. The token generation is as follows:

View code

 [Serializable]  Public   Class  Objecttwo: myobject, ideserializationcallback {  Public   Int  Num;  Public  Objecttwo ():  Base  (){}  Protected  Objecttwo (serializationinfo Si, streamingcontext context ):  Base (Si, context) {num = Si. getint32 ( "  Num  "  );} [Securitypermissionattribute (securityaction. Demand, serializationformatter = True  )]  Public   Override   Void  Getobjectdata (serializationinfo Si, streamingcontext context ){  Base . Getobjectdata (Si, context); Si. addvalue (  "  Num  "  , Num );}  # Region Ideserializationcallback members Public   Void Ondeserialization ( Object  Sender) {num = N1 + N2 ;}  # Endregion  } 

Note: In the getobjectdata method, it is best to add a security constraint.

[Securitypermissionattribute (securityaction. Demand, serializationformatter
= True)].

[System. xml. serialization. xmlelementattribute (Order = 0)] adequacy is added to adequacy and can be set

The sequence of serialization.

5. In order to perform certain operations after the reverse sequence number is reached, we need to specify the ideserializationcallback interface for this type.

In this way, you can perform an operation after deserializing the data volume. After deserialization is complete, the compute uses the base class data to generate the data records and add them.

 
# RegionIdeserializationcallback membersPublic VoidOndeserialization (ObjectSender) {num= N1 +N2 ;}# Endregion

6. When saving the serialization, The deserialization data can be used to bear any type of stream. For example, filestream file stream. Memory stream.

7. During the generation of serialization and deserialization, binary or SOP can be specified. For example, soapformatter binaryformatter. Serialization and

Deserialization of data records in the same format.

8. You only need to add [nonserialized] To the deserialization of attributes that you do not want to serialize. This encoding will not be serialized during serialization.

9. Big Data serialization

9.1 definition server-side return data

[Xmltype (namespace ="Http://my.namespace.com/data")]Public ClassContent {[xmlelement]Public StringName; [xmlelement]Public StringExtension; [xmlelement]Public Byte[] Data ;}

Byte [] is used to enlarge the data size.

The 9.2 client must accept the ixmlserializable interface.

The token generation is as follows:

View code

 Public   Partial   Class  Content: ixmlserializable {  Private  Stream stream;  Private   String  Streamfilename;  Public  Stream datastream {  Set { This . Stream =Value ;}}  Public  Stream getdatastream (){  Return File. openread ( This  . Streamfilename );}  Public  XMLSCHEMA getschema (){  Return   Null  ;}  Public   Void  Readxml (xmlreader reader) {reader. readstartelement (); //  Wrapping Element          This . Name = Reader. readelementcontentasstring ();  This . Extension = Reader. readelementcontentasstring ();  String Tempfilename = Path. gettempfilename ();  This . Streamfilename = Tempfilename;  Using (Filestream FS = File. Create (tempfilename )){ Byte [] Buffer = New   Byte [ 1000  ];  Int  Bytesread; reader. readstartelement ();  Do  {Bytesread = Reader. readcontentasbase64 (buffer, 0  , Buffer. Length); FS. Write (buffer,  0  , Bytesread );} While (Bytesread> 0  ); Reader. readendelement ();} reader. readendelement ();  //  Wrapping Element  }  Public   Void  Writexml (xmlwriter writer) {writer. writestartelement (  "  Name  "  ); Writer. writevalue (  This . Name); writer. writeendelement (); writer. writestartelement (  "  Extension  "  ); Writer. writevalue (  This  . Extension); writer. writeendelement (); writer. writestartelement (  "  Data  "  ); Xmldictionarywriter dictwriter = Writer As  Xmldictionarywriter; Bool Ismtom = dictwriter! = Null & Dictwriter Is  Ixmlmtomwriterinitializer;  If  (Ismtom) {dictwriter. writevalue (  New Mystreamprovider ( This  . Stream ));}  Else  {  //  Fall back to the original behavior             Byte [] Buffer = New   Byte [ 1000  ];  Int  Bytesread;  Do  {Bytesread = This . Stream. Read (buffer, 0  , Buffer. Length );  If (Bytesread> 0 ) {Writer. writebase64 (buffer,  0  , Bytesread );}}  While (Bytesread> 0  );} Writer. writeendelement ();}  Class  Mystreamprovider: istreamprovider {stream;  Public  Mystreamprovider (Stream stream ){  This . Stream = Stream ;} Public  Stream getstream (){  Return   This  . Stream ;}  Public   Void  Releasestream (Stream stream ){}}} 

The replacement of the 9.3 guest terminal is as follows:

View code

Someserviceclient c = New  Someserviceclient ();  Byte [] Filecontents =New   Byte [ 100  ];  For ( Int I = 0 ; I <filecontents. length; I ++ ) {Filecontents [I] = ( Byte ) '  A  '  ;} Requestclass request = New Requestclass {ID = "  ID  "  , Token = "  Token  "  , Content = New  Content {name = "  File  " , Extension = "  EXT  "  ,  //  Data = filecontents, Datastream = New  Memorystream (filecontents),},}; C. sendrequest (request); responseclass Resp = C. getresponse ( 234  ); Console. writeline (resp. content. Name); stream datastream =Resp. content. getdatastream ();  String TEXT = New Streamreader (datastream). readtoend ();

 

 

 

 

 

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.