C#结构体序列化

C#结构体序列化
C#结构体定义:
[Serializable]
    
struct  DR2DE_Send
    
{
        
public UInt32 AA;
        
public UInt32 BB;
        
public UInt16 CC;   //! Encryption method.
        public UInt16 DD;            //! message version  // used to decide message version.
        public UInt32 EE;           //! message id.
        public UInt32 FF;          //! Message order. it is a increseable integer and set when checkEncrypt called and when ReturnMessage is set.
        public Byte GG;
        
public Byte LL;
        
public char[] MM;
    }

序列化操作:
byte [] sendMsg  =   new   byte [ 1024 ];
            BinaryFormatter bf 
=   new  BinaryFormatter();
            MemoryStream stream 
=   new  MemoryStream();
            DR2DE_Send dr2deMessage;
            dr2deMessage.AA
=  (UInt32)( 0 );
            dr2deMessage.BB
=  (UInt16)( 0 );
            dr2deMessage.CC
= (UInt32)( 0 );
            dr2deMessage.DD
=  (UInt16)( 0 );
            dr2deMessage.EE
=   " Hello " .ToCharArray();
            dr2deMessage.FF
=   40010 ;
            dr2deMessage.GG
=  (UInt32)( 22   +  dr2deMessage.data.Length);
            dr2deMessage.HH
=   1 ;
            dr2deMessage.II
=   1 ;

            bf.Serialize(stream, dr2deMessage);
            sendMsg 
=  stream.ToArray();
sendMsg就可以放到sock中发送出去了。

你可能感兴趣的:(C#结构体序列化)