Byte stream into an object method

Today, when the byte stream into an object, will always find an error, later identified because the Index To set the flow as a starting position, because after serialization Index moved

End of the stream, so just being given. code show as below:


public static T  DeSerializer<T>(byte[] buffer, int length)
        {
            try
            {
                string mTextReceived = Encoding.UTF8.GetString(buffer, 0, length);
                SocketDataPack sdp = GetSocketDataPackFromString(mTextReceived);

                // MemoryStream object provides a method you can create the need for IO Stream of
                MemoryStream Stream =  new new  MemoryStream ();
                BinaryFormatter Formatter =  new new  BinaryFormatter ();
                . Formatter the Serialize (Stream, sdp);

                Index // set the starting position of the stream
                stream.Position = 
0 ;

                //  ensure that updates to a Stream writer;
                . Stream the Flush ();
                Object  obj = Formatter. The Deserialize (Stream);
               
                IF  (obj ==  null )
                {
                    return  default (T);
                }
                return  (T) obj;

                
            }
            the catch  ( EX Exception)
            {
                the throw  EX;
            }
        }

Reproduced in: https: //www.cnblogs.com/kevinGao/archive/2011/12/16/2294470.html

Guess you like

Origin blog.csdn.net/weixin_34104341/article/details/93359114
Recommended