Skip to content

Instantly share code, notes, and snippets.

@yangruihan
Created September 22, 2017 06:23
Show Gist options
  • Select an option

  • Save yangruihan/8d2feb796c3c31fd8355d336b9e799cf to your computer and use it in GitHub Desktop.

Select an option

Save yangruihan/8d2feb796c3c31fd8355d336b9e799cf to your computer and use it in GitHub Desktop.
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