- A+
所属分类:.NET技术
1、对象转为byte[] 数据
/// <summary> /// 将对象转化为字节数字 /// </summary> /// <param name="obj">需要转化对象</param> /// <returns></returns> public static byte[] ConvertToBytes(this object obj) { using (var stream = new MemoryStream()) { var formatter=new BinaryFormatter(); formatter.Serialize(stream,obj); return stream.GetBuffer(); } }
2、对象转为Stream类型
/// <summary> /// 对象转为数据流 /// </summary> /// <param name="obj">需要转化的对象</param> /// <returns></returns> public static MemoryStream ConvertToStream(this object obj) { using (var stream = new MemoryStream()) { var formatter = new BinaryFormatter(); formatter.Serialize(stream, obj); return stream; } }