序列化包含字典成员的类扩展到我的早期问题,我决定序列化我的config文件类,它运行得很好。我现在想要存储要映射的驱动器字符的关联数组(键是驱动器字母,值是网络路径),并尝试使用Dictionary, HybridDictionary,和Hashtable但是,在调用时总是会收到以下错误ConfigFile.Load()或ConfigFile.Save():反映类型‘App.ConfigFile’时出错。[Snip]System.NotSupportdException:无法序列化成员App.Configfile.mempdDrives[Snip]根据我所读过的字典和HashTables可以被序列化,那么我做错了什么呢?[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();
}
}
}}
3 回答
- 3 回答
- 0 关注
- 303 浏览
添加回答
举报
0/150
提交
取消