序列化包含字典成员的类

编程入门 行业动态 更新时间:2024-10-11 13:25:38
本文介绍了序列化包含字典成员的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

我现在想存储要映射的驱动器号的关联数组(键是驱动器号,值是网络路径)并尝试使用 Dictionary、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'.[剪辑]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.

更多推荐

序列化包含字典成员的类

本文发布于:2023-11-22 04:52:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616041.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字典   成员   序列化

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!