序列化.NET词典 [英] Serializing .NET dictionary

查看:173
本文介绍了序列化.NET词典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/495647/serialize-class-containing-dictionary-member\">Serialize包含类字典成员

我可以序列化一个解释?

Can I serialize a Dictionary?

推荐答案

哪个序列化API?

例如,的DataContractSerializer 可以处理字典,尤其是与(可选) [CollectionDataContract] 标记。 protobuf网将处理它们(如下图)。其他人可能不...

For example, DataContractSerializer can handle dictionaries, especially with the (optional) [CollectionDataContract] markup. protobuf-net will handle them (below). Others may not...

    var data = new Dictionary<string, int>();
    data.Add("abc", 123);
    data.Add("def", 456);

    var clone = Serializer.DeepClone(data);
    Console.WriteLine(clone["abc"]);
    Console.WriteLine(clone["def"]);

由于意志的BinaryFormatter

    using (MemoryStream ms = new MemoryStream())
    {
        var bf = new BinaryFormatter();
        bf.Serialize(ms, data);
        ms.Position = 0;
        clone = (Dictionary<string, int>) bf.Deserialize(ms);
    }

这篇关于序列化.NET词典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆