/// <summary> /// 直接保存实体数据,如果文件已经存在则覆盖(不会影响内存数据) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entitys"></param> /// <returns></returns> public bool SaveEntity <T>(T[] entitys) where T : EntityBase, new() { Type t = typeof(T); if (entitys == null) { this.WriteLog(t.FullName + " 数据为空!"); return(false); } int count = entitys.Length; if (count > 0) { this.WriteLog("开始写入数据,条数:" + count); string fileName = this.FilePath + "\\" + t.FullName + ".pmdb"; byte[] buffer = PdfNetSerialize <T> .BinarySerialize(entitys); using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write)) { fs.Write(buffer, 0, buffer.Length); fs.Flush(); fs.Close(); } this.WriteLog("保存数据 " + fileName + " 成功!"); return(true); } return(false); }
public void 序列号() { SODUser model = new SODUser(); model.LogName = "aa"; //序列化 byte[] buffer = PdfNetSerialize.BinarySerialize(model); string tempString = Convert.ToBase64String(buffer); //反序列化(为实现) byte[] buffer2 = Convert.FromBase64String(tempString); SODUser customer2 = (SODUser)PdfNetSerialize.BinaryDeserialize(buffer2, typeof(SODUser)); }
/// <summary> /// 从数据文件载入实体数据(不会影响内存数据),建议使用Get的泛型方法 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public List <T> LoadEntity <T>() where T : EntityBase, new() { Type t = typeof(T); string fileName = this.FilePath + "\\" + t.FullName + ".pmdb"; if (File.Exists(fileName)) { byte[] buffer = null; using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { long length = fs.Length; buffer = new byte[length]; fs.Read(buffer, 0, (int)length); fs.Close(); } T[] result = PdfNetSerialize <T> .BinaryDeserializeArray(buffer); this.WriteLog("加载数据 " + fileName + " 成功!"); return(result.ToList()); } return(null); }