C# 对象二进制序列化
2012-06-06 11:19
405 查看
using System.Runtime.Serialization.Formatters.Binary;
public class SerializationUnit
{
/// <summary>
/// 把对象序列化为字节数组
/// </summary>
public static byte[] SerializeObject(object obj)
{
if (obj == null)
return null;
MemoryStream ms = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, obj);
ms.Position = 0;
byte[] bytes = new byte[ms.Length];
ms.Read(bytes, 0, bytes.Length);
ms.Close();
return bytes;
}
/// <summary>
/// 把字节数组反序列化成对象
/// </summary>
public static object DeserializeObject(byte[] bytes)
{
object obj = null;
if (bytes == null)
return obj;
MemoryStream ms = new MemoryStream(bytes);
ms.Position = 0;
BinaryFormatter formatter = new BinaryFormatter();
obj = formatter.Deserialize(ms);
ms.Close();
return obj;
}
}
public class SerializationUnit
{
/// <summary>
/// 把对象序列化为字节数组
/// </summary>
public static byte[] SerializeObject(object obj)
{
if (obj == null)
return null;
MemoryStream ms = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, obj);
ms.Position = 0;
byte[] bytes = new byte[ms.Length];
ms.Read(bytes, 0, bytes.Length);
ms.Close();
return bytes;
}
/// <summary>
/// 把字节数组反序列化成对象
/// </summary>
public static object DeserializeObject(byte[] bytes)
{
object obj = null;
if (bytes == null)
return obj;
MemoryStream ms = new MemoryStream(bytes);
ms.Position = 0;
BinaryFormatter formatter = new BinaryFormatter();
obj = formatter.Deserialize(ms);
ms.Close();
return obj;
}
}
相关文章推荐
- C# 对象二进制序列化
- C# .Net 对象二进制序列化
- C#把对象序列化成二进制文件
- C# 对象二进制序列化
- C#复杂对象序列化,推荐二进制
- C# 以二进制序列化DataSet对象
- C#之序列化对象(二进制方式序列化对象)
- c#对象序列化(二进制序列化)BinaryFormatter【转】
- C#对象序列化(2)
- C# 二进制BinaryFormatter进行序列化与反序列化
- Serializable在C#中的作用.NET 中的对象序列化
- C#序列化/反序列化对象.将对象写入文件,从文件读取对象.
- [Serializable]在C#中的作用-NET 中的对象序列化
- [Serializable]在C#中的作用-NET 中的对象序列化
- c#将对象序列化为字符串和将字符串反序列化为对象
- (转)Serializable在C#中的作用.NET 中的对象序列化
- AMF序列化为对象和AMF序列化为二进制字节流
- C#将对象序列化成JSON字符串
- .net对象二进制序列化
- C#实现序列化对象到XML文档与反序列化