序列化包含字典成员的类 [英] Serialize Class containing Dictionary member

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

问题描述

扩展我的早期问题,我决定(反)序列化我的配置文件类,效果很好.

Expanding upon my earlier problem, I've decided to (de)serialize my config file class which worked great.

我现在想存储要映射的驱动器号的关联数组(键是驱动器号,值是网络路径)并尝试使用 DictionaryHybridDictionary, 和 Hashtable 为此,但在调用 ConfigFile.Load()ConfigFile.Save() 时,我总是收到以下错误:

I now want to store an associative array of drive letters to map (key is the drive letter, value is the network path) and have tried using Dictionary, HybridDictionary, and Hashtable for this but I always get the following error when calling ConfigFile.Load() or ConfigFile.Save():

有一个错误反映类型'App.ConfigFile'.[剪辑]System.NotSupportedException:不能序列化成员App.Configfile.mappedDrives [snip]

There was an error reflecting type 'App.ConfigFile'. [snip] System.NotSupportedException: Cannot serialize member App.Configfile.mappedDrives [snip]

从我读过的内容来看,字典和哈希表可以被序列化,那么我做错了什么?

From what I've read Dictionaries and HashTables can be serialized, so what am I doing wrong?

[XmlRoot(ElementName="Config")]
public class ConfigFile
{
    public String guiPath { get; set; }
    public string configPath { get; set; }
    public Dictionary<string, string> mappedDrives = new Dictionary<string, string>();

    public Boolean Save(String filename)
    {
        using(var filestream = File.Open(filename, FileMode.OpenOrCreate,FileAccess.ReadWrite))
        {
            try
            {
                var serializer = new XmlSerializer(typeof(ConfigFile));
                serializer.Serialize(filestream, this);
                return true;
            } catch(Exception e) {
                MessageBox.Show(e.Message);
                return false;
            }
        }
    }

    public void addDrive(string drvLetter, string path)
    {
        this.mappedDrives.Add(drvLetter, path);
    }

    public static ConfigFile Load(string filename)
    {
        using (var filestream = File.Open(filename, FileMode.Open, FileAccess.Read))
        {
            try
            {
                var serializer = new XmlSerializer(typeof(ConfigFile));
                return (ConfigFile)serializer.Deserialize(filestream);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.ToString());
                return new ConfigFile();
            }
        }
    }
}

推荐答案

您不能序列化实现 IDictionary 的类.查看此链接.

You can't serialize a class that implements IDictionary. Check out this link.

问:为什么我不能序列化哈希表?

Q: Why can't I serialize hashtables?

A:XmlSerializer 无法处理实现 IDictionary 的类界面.这部分是由于进度限制,部分原因是哈希表没有的事实在 XSD 类型中有一个对应物系统.唯一的解决办法是实现一个自定义的哈希表不实现 IDictionary界面.

A: The XmlSerializer cannot process classes implementing the IDictionary interface. This was partly due to schedule constraints and partly due to the fact that a hashtable does not have a counterpart in the XSD type system. The only solution is to implement a custom hashtable that does not implement the IDictionary interface.

所以我认为您需要为此创建自己的词典版本.检查这个其他问题.

So I think you need to create your own version of the Dictionary for this. Check this other question.

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

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