我在这段代码中找不到问题。我试图找到一种特定类型的属性,并在其上调用一个方法。
职能如下:
private string GetLangTranslator(object root)
{
var properties = root.GetType().GetProperties();
foreach (var property in properties)
{
if (typeof(MultiLanguage) == property.PropertyType)
{
MethodInfo m = property.PropertyType.GetMethod("Translate");
return m.Invoke(property.PropertyType, new object[] {Value1}) as string;
}
}
return null;
}例外情况如下:
System.Reflection.TargetException: 'Object does not match target type.'发布于 2017-04-06 09:50:39
您应该:
object propValue = property.GetValue(root);
return m.Invoke(propValue, new object[] {Value1}) as string;Invoke的第一个参数是要调用方法/属性的对象的实例.因此需要首先检索属性的值。
https://stackoverflow.com/questions/43251571
复制相似问题