I can't find the problem in this code. I'm trying to find a particular kind of property and invoke a method on it.
The function is the following:
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;
}
And the exception is the following:
System.Reflection.TargetException: 'Object does not match target type.'
You should:
object propValue = property.GetValue(root);
return m.Invoke(propValue, new object[] {Value1}) as string;
The first parameter of Invoke
is the instance of the object you want to call the method/property... So need to retrieve the value of the property first.