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

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

问题描述

扩大在我的更早问题,我决定(德)序列化我的配置这伟大的工作文件类。

我现在要存储的驱动器号关联数组映射(关键是驱动器号,值是网络路径),并使用已尝试词典 HybridDictionary的的Hashtable 的这一点,但我总是打电话时收到以下错误 ConfigFile.Load() ConfigFile.Save()

  

有一个错误的反射式   App.ConfigFile。 [剪断]   System.NotSupportedException:不能   序列成员   App.Configfile.mappedDrives [剪断]

这是我读过的字典和哈希表可以被序列化,所以我是什么做错了吗?

  [XmlRoot(的ElementName =配置)
公共类ConfigFile实现
{
    公共字符串guiPath {获得;组; }
    公共字符串configPath来{获得;组; }
    公共字典<字符串,字符串> mappedDrives =新字典<字符串,字符串>();

    公布尔保存(字符串文件名)
    {
        使用(VAR FILESTREAM = File.open方法(文件名,FileMode.OpenOrCreate,FileAccess.ReadWrite))
        {
            尝试
            {
                无功序列化=新的XmlSerializer(typeof运算(ConfigFile实现));
                serializer.Serialize(文件流,这一点);
                返回true;
            }赶上(例外五){
                的MessageBox.show(e.Message);
                返回false;
            }
        }
    }

    公共无效addDrive(字符串drvLetter,字符串路径)
    {
        this.mappedDrives.Add(drvLetter,路径);
    }

    公共静态ConfigFile实现负载(字符串文件名)
    {
        使用(VAR FILESTREAM = File.open方法(文件名,FileMode.Open,FileAccess.Read))
        {
            尝试
            {
                无功序列化=新的XmlSerializer(typeof运算(ConfigFile实现));
                返程(ConfigFile实现)serializer.Deserialize(文件流);
            }
            赶上(例外前)
            {
                的MessageBox.show(ex.Message + ex.ToString());
                返回新ConfigFile实现();
            }
        }
    }
}
 

解决方案

您不能序列实现IDictionary的一类。看看这个链接

  

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

?      

答:XmlSerializer的无法处理   课程实施的IDictionary   接口。这部分是由于   进度约束部分是由于   一个哈希表没有事实   有在XSD类型的对应   系统。唯一的解决办法是,   实现自定义的哈希表,做   不执行的IDictionary   接口。

所以,我认为你需要创建自己的版本的词典这一点。勾选此等问题

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

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():

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();
            }
        }
    }
}

解决方案

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

Q: Why can't I serialize hashtables?

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天全站免登陆