2017-04-06 3 views
0

이 코드에서 문제를 찾을 수 없습니다. 나는 특정 종류의 속성을 찾고 그것에 대한 메서드를 호출하려고합니다.C# MethodInfo Invoke

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

답변

1

당신이해야 :

object propValue = property.GetValue(root); 
return m.Invoke(propValue, new object[] {Value1}) as string; 

Invoke의 첫 번째 매개 변수의 인스턴스 메서드 또는 속성을 호출 할 개체 ... 값을 검색해야합니다. 먼저 재산.

+0

고맙습니다! 그것은 완벽하게 작동합니다. –