在我的 之前的问题的基础上,我决定(反)序列化我的配置文件类,它工作得很好。
我现在想要存储一个关联数组的驱动器号来映射(key 是驱动器号,value 是网络路径) ,并且尝试过使用 Dictionary
、 HybridDictionary
和 Hashtable
,但是在调用 ConfigFile.Load()
或者 ConfigFile.Save()
时总是会出现以下错误:
有一个错误反射类型 ‘ App. ConfigFile’ 异常: 不能 序列化成员 应用程序
从我所阅读的字典和哈希表可以序列化,那么我做错了什么?
[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();
}
}
}
}