序列化包含字典成員的類擴(kuò)展到我的早期問題,我決定序列化我的config文件類,它運(yùn)行得很好。我現(xiàn)在想要存儲要映射的驅(qū)動(dòng)器字符的關(guān)聯(lián)數(shù)組(鍵是驅(qū)動(dòng)器字母,值是網(wǎng)絡(luò)路徑),并嘗試使用Dictionary, HybridDictionary,和Hashtable但是,在調(diào)用時(shí)總是會收到以下錯(cuò)誤ConfigFile.Load()或ConfigFile.Save():反映類型‘App.ConfigFile’時(shí)出錯(cuò)。[Snip]System.NotSupportdException:無法序列化成員App.Configfile.mempdDrives[Snip]根據(jù)我所讀過的字典和HashTables可以被序列化,那么我做錯(cuò)了什么呢?[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();
}
}
}}
序列化包含字典成員的類
當(dāng)年話下
2019-07-17 14:30:07