从父类对象获取对象

时间:2017-03-29 11:01:59

标签: c#

我有课

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

1 个答案:

答案 0 :(得分:3)

使用XmlSerializer

创建IPTable
XmlSerializer serializer = new XmlSerializer(typeof(IPTable));
StreamReader textReader = new StreamReader(xmlFileName);
return (IPTable)serializer.Deserialize(textReader);