Created
September 22, 2017 06:23
-
-
Save yangruihan/8d2feb796c3c31fd8355d336b9e799cf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Runtime.Serialization; | |
| using System.Runtime.Serialization.Formatters.Binary; | |
| using System.IO; | |
| public class SerializeHelper | |
| { | |
| /// <summary> | |
| /// 序列化 | |
| /// </summary> | |
| /// <returns>The message queue.</returns> | |
| /// <param name="msgQueue">Message queue.</param> | |
| public static byte[] SerializeObj<T>(T obj) | |
| { | |
| IFormatter formatter = new BinaryFormatter(); | |
| MemoryStream stream = new MemoryStream(); | |
| formatter.Serialize(stream, obj); | |
| stream.Position = 0; | |
| byte[] buffer = new byte[stream.Length]; | |
| stream.Read(buffer, 0, buffer.Length); | |
| stream.Flush(); | |
| stream.Close(); | |
| return buffer; | |
| } | |
| /// <summary> | |
| /// 反序列化 | |
| /// </summary> | |
| /// <returns>The message queue.</returns> | |
| /// <param name="bytes">Bytes.</param> | |
| public static T DesrializeObj<T>(byte[] bytes) | |
| { | |
| T obj; | |
| IFormatter formatter = new BinaryFormatter(); | |
| MemoryStream stream = new MemoryStream(bytes); | |
| obj = (T)formatter.Deserialize(stream); | |
| stream.Flush(); | |
| stream.Close(); | |
| return obj; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment