我正在使用一种通用方法来反序列化xml文档取决于包含。它试图反序列化所有可能的情况。
这是我的代码段:
private static Dictionary<Type, byte> getMessageDictionary() {
Dictionary<Type, byte> typesIO = new Dictionary<Type, byte>();
typesIO.Add(typeof (Type1), 1);
typesIO.Add(typeof (Type2), 11);
typesIO.Add(typeof (Type3), 12);
return typesIO;
}
public static object GetContainer(XmlDocument xd) {
foreach(KeyValuePair<Type, byte> item in getMessageDictionary()) {
try {
Type p = item.Key;
var z = Utils.XmlDeserialize<p> (xd.OuterXml);
return z;
} catch {
continue;
}
}
return null;
}
但是编译器说p
找不到类型或名称空间名称。我会错过using
指令或程序集参考吗?什么地方出了错?
p
是包含对Type
实例的引用的变量,但是您尝试将其用作类型参数。
要执行您想要的操作,您将需要使用反射调用该方法:
Type p = item.Key;
var method = typeof(Utils).GetMethod("XmlDeserialize").MakeGenericMethod(p);
var z = (XmlDocument)method.Invoke(null, new object[] { xd.OuterXml });
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句