国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

protobuf對象二進制序列化存儲(詳解)

2020-01-24 00:45:48
字體:
來源:轉載
供稿:網友

首先下載protobuf庫,可以用Nuget。

demo:

using System;namespace Tools{  public class BufHelp  {    /// <summary>    /// 對象鎖    /// </summary>    private readonly static Object Locker = new Object();    ///// <summary>    ///// 讀寫分離鎖    ///// </summary>    ///// <remarks>aaaaa</remarks>    //private static ReaderWriterLockSlim rwl = new ReaderWriterLockSlim();    /// <summary>    /// 序列化-表字段業務信息    /// </summary>    public static bool ProtoBufSerialize<T>(T model, string filename) where T : class    {      try      {        string binpath = 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<T>(file, model);            return true;          }        }      }      catch      {        return false;      }    }    public static T ProtoBufDeserialize<T>(string filename) where T : class    {      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<T>(file);            return result;          }        }      }      return default(T);    }  }}/// <summary>    /// 序列化    /// </summary>    public static string Serialize<T>(T t) where T : class    {      using (MemoryStream ms = new MemoryStream())      {        ProtoBuf.Serializer.Serialize<T>(ms, t);        return Encoding.UTF8.GetString(ms.ToArray());      }    }    /// <summary>    /// 反序列化    /// </summary>    public static T DeSerialize<T>(string content) where T : class    {      using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(content)))      {        T t = ProtoBuf.Serializer.Deserialize<T>(ms);        return t;      }    }

以上這篇protobuf對象二進制序列化存儲(詳解)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宝山区| 雷山县| 望奎县| 安吉县| 澄江县| 喀喇| 五指山市| 通化市| 紫阳县| 揭东县| 嘉定区| 安康市| 平山县| 灯塔市| 许昌市| 盖州市| 涞源县| 湖州市| 汉阴县| 武宣县| 永川市| 长治市| 禄丰县| 昌邑市| 广灵县| 平定县| 图们市| 肃北| 丰城市| 铜梁县| 孝感市| 泰州市| 仁寿县| 夏河县| 凤庆县| 大方县| 铜山县| 永川市| 马山县| 西昌市| 响水县|