1

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.'

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.