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

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

问题描述

展开我的早期问题,我决定(de)序列化我的配置文件类工作得很好。

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

我现在想要存储一组关联的驱动器号映射(关键是驱动器盘符,值是网络路径),并尝试使用字典 HybridDictionary 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'的错误。 [snip]
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.

所以我想你需要为此创建自己的版本的Dictionary。检查此其他问题

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

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

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