C# 对象二进制序列化
2011-01-04 16:40
399 查看
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#对象序列化(二进制序列化)BinaryFormatter【转】
- C# 对象二进制序列化
- C#实现对象的Xml格式序列化及反序列化
- C#对象序列化与反序列化
- C#对象序列化与反序列化
- C#中XML与对象之间的序列化、反序列化
- C# 使用XML序列化对象(二)
- Serializable在C#中的作用.NET 中的对象序列化
- C#实现对象的Xml格式序列化及反序列化
- [转载]C#对象序列化与反序列化
- C#实现对象的Xml格式序列化及反序列化
- C# 对象的序列化与反序列化
- Serializable在C#中的作用.NET 中的对象序列化
- C# 调用Webservice并传递序列化对象