我有课
public class IPTable : SerializableDictionary<string, string>
{
...
}
尝试反序列化IPTable
:
XmlSerializer serializer = new XmlSerializer(typeof(SerializableDictionary<string, string>));
StreamReader textReader = new StreamReader(xmlFileName);
SerializableDictionary<string, string> ip =( SerializableDictionary < string, string>) serializer.Deserialize(textReader);
return (IPTable)ip;
在将SerializableDictionary<string, string>
投射到IPTable
时,下面有例外。
Message = "Unable to cast object of type 'SerializableDictionary`2[System.String,System.String]' to type 'IPTable'."
如何反序列化为IPTable
?
答案 0 :(得分:3)
使用XmlSerializer
IPTable
XmlSerializer serializer = new XmlSerializer(typeof(IPTable));
StreamReader textReader = new StreamReader(xmlFileName);
return (IPTable)serializer.Deserialize(textReader);