CSLA4.0框架中使用序列化与反序列化serialize
来源:互联网 发布:手机游戏加速器软件 编辑:程序博客网 时间:2024/06/15 05:33
CSLA4.0框架中使用序列化与反序列化,序列化的结果保存到SQL数据库中
/// <summary> /// 把对象序列化为字节数组 /// </summary> public static byte[] SerializeObject(object obj) { if (obj == null) return null; MemoryStream ms = new MemoryStream(); var formatter = Csla.Serialization.SerializationFormatterFactory.GetFormatter(); 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; var formatter = Csla.Serialization.SerializationFormatterFactory.GetFormatter(); obj = formatter.Deserialize(ms); ms.Close(); return obj; }
使用:
/// 通用任务查询条件 /// </summary> [Serializable()] public class TaskSearchCriteria : Csla.CriteriaBase<TaskSearchCriteria> { public static PropertyInfo<string> MethodNameProperty = RegisterProperty<string>(c => c.MethodName); public string MethodName { get { return ReadProperty(MethodNameProperty); } set { LoadProperty(MethodNameProperty, value); } } public static PropertyInfo<string> ProductNoProperty = RegisterProperty<string>(c => c.ProductNo); public string ProductNo { get { return ReadProperty(ProductNoProperty); } set { LoadProperty(ProductNoProperty, value); } }
}
data.Data = MyUtilities.SerializeObject(Criteria);
var criteria = MyUtilities.DeserializeObject(d.Data) as Library.TaskSearchCriteria;