最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
protobuf对象二进制序列化存储
时间:2022-06-25 07:53:22 编辑:袖梨 来源:一聚教程网
首先下载protobuf库,可以用Nuget。
demo:
代码如下 | 复制代码 |
usingSystem;
namespaceTools { publicclassBufHelp { /// /// 对象锁 /// privatereadonlystaticObject Locker =newObject(); ///// ///// 读写分离锁 ///// ///// //private static ReaderWriterLockSlim rwl = new ReaderWriterLockSlim();
/// /// 序列化-表字段业务信息 /// publicstaticboolProtoBufSerialize { try { stringbinpath = Config.KeyCenter.KeyBaseDirectory +@"Config";
if(!System.IO.Directory.Exists(binpath)) System.IO.Directory.CreateDirectory(binpath);
lock(Locker) { using(var file = System.IO.File.Create(binpath + filename)) { ProtoBuf.Serializer.Serialize returntrue; } } } catch { returnfalse; } }
publicstaticT ProtoBufDeserialize { var dbpath = Config.KeyCenter.KeyBaseDirectory +@"Config"+ filename;
if(System.IO.File.Exists(dbpath)) { lock(Locker) { using(var file = System.IO.File.OpenRead(dbpath)) { var result = ProtoBuf.Serializer.Deserialize returnresult; } } }
returndefault(T); } } }/// /// 序列化 /// publicstaticstringSerialize { using(MemoryStream ms =newMemoryStream()) { ProtoBuf.Serializer.Serialize returnEncoding.UTF8.GetString(ms.ToArray()); } } /// /// 反序列化 /// publicstaticT DeSerialize { using(MemoryStream ms =newMemoryStream(Encoding.UTF8.GetBytes(content))) { T t = ProtoBuf.Serializer.Deserialize returnt; } } |
相关文章
- 王者荣耀侦探能力大测试攻略 王者荣耀侦探能力大测试怎么过 11-22
- 无期迷途主线前瞻兑换码是什么 11-22
- 原神欧洛伦怎么培养 11-22
- 炉石传说网易云音乐联动怎么玩 11-22
- 永劫无间手游确幸转盘怎么样 11-22
- 无期迷途主线前瞻兑换码是什么 无期迷途主线前瞻直播兑换码介绍 11-22