如何在c#

时间:2018-08-09 09:52:12

标签: c#

如何在c#中读取和映射序列化的二进制文件?

我使用BinaryFormatter序列化我的DataTree类。 这是我要序列化的目标类。

[Serializable()]
public class AppData
{
     public string name = "";
     public int value = 0;
     public AppData() {}
     public AppData(string _name, int _value)
     {
         name = _name;
         value = _value;
     }
}


public enum TreeType
{
    Type1,
    Type2,
    Type3
}
[Serializable()]
public class DataTree
{
    public TreeType Type { get; set; }
    public string CategoryName { get; set; }
    public List<AppData> AppDataInfos { get; set; }
    public Dictionary<string, AppDataTree> ChildTrees { get; set; }

    public AppDataTree(TreeType type, string categoryName)
    {
        Type = type;
        CategoryName = categoryName;
        AppDataInfos = new List<AppData>();
        ChildTrees = new Dictionary<string, DataTree>();
    }
    public DataTree FindChildTree(string categoryName)
    {
        if (ChildTrees.ContainsKey(categoryName) == false)
            return null;
        return ChildTrees[categoryName];
    }


}

它具有字典和列表成员。 我序列化了DataTree实例并保存到二进制文件以进行缓存。

然后我要读取此二进制序列化的数据文件,并仅映射到内存而无需反序列化。因为反序列化太慢。(可悲的是,它非常非常慢。它比没有缓存要慢) (由于缓存,我将其序列化并保存为二进制文件)

也许如果我捕获DataTree实例并保存到二进制文件并读取它以映射到内存,我该怎么办?还是有人有主意?像快速序列化/反序列化解决方案一样……等等。

0 个答案:

没有答案