在webservice中可能需要用到,放到一个类中: using System; using System.Collections.Generic; using System.Text; using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization; using System.IO; namespace Com.DRPENG.NhibernateFactory.Common { /// <summary> /// 格式化类 /// </summary> public class DataFormatter { /// <summary> /// 序列化对象成二进制流 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static byte[] SerializeOperate(object obj) { byte[] bs; BinaryFormatter formatter = new BinaryFormatter(); try { if (obj != null) { MemoryStream ms = new MemoryStream(); //这里是关键 formatter.Serialize(ms, obj); //这里可以进行序列化 bs = ms.ToArray(); } else { bs = new byte[0]; } } catch { throw; } return bs; } public static object DeserializeOperate(byte[] bs) { object obj = null; try { MemoryStream ms = new MemoryStream(bs); BinaryFormatter formatter = new BinaryFormatter(); obj = formatter.Deserialize(ms); //反序列化 } catch { throw; } return obj; } } } 在webservice中也可以直接使用 /// <summary> /// 根据ID查询用户信息 /// </summary> /// <param name="id"></param> /// <returns></returns> [WebMethod] public byte[] GetRetrieveUserInfo(byte[] id) { try { MemoryStream ms = new MemoryStream(id); BinaryFormatter formatter = new BinaryFormatter(); Guid userId = (Guid)formatter.Deserialize(ms); //反序列化 SysUserInfo user = new SysUserInfo(); user = operate.GetEntity<SysUserInfo>(userId); byte[] bs = DataFormatter.SerializeOperate(user); return bs; } catch { throw; } } 这里做个留念!