2016-07-04 3 views
2

메서드 이름과 메서드 매개 변수가있는 경우 메서드에 대해 MethodCallExpression을 어떻게 만듭니 까? 타입 의메소드의 MethodCallExpression은 어떻게 만들 수 있습니까?

되지 않은 예외 ': 여기

var methodInfo = obj.GetType().GetMethod("HandleEventWithArg"); 
var body = Expression.Call(Expression.Constant(methodInfo), methodInfo.GetType().GetMethod("Invoke"), argExpression); 

예외이다 : 여기
public void HandleEventWithArg(int arg) 
{ 

} 

제가 가지고있는 코드이다 : 여기

은 예시적인 방법으로 System.Reflection.AmbiguousMatchException 'mscorlib.dll에서 발생했습니다

추가 정보 : 모호한 일치를 찾았습니다.

+0

메서드 이름을 사용하면 네임 스페이스와 결합해야합니다. –

+0

@ moller1111 나는 그것이 사실이 아니라고 확신한다. – nawfal

+1

메소드 이름에 대해 일치하는 항목이 여러 개 발견 될 수 있으므로 예외가 발생합니다. 기본적으로 동일한 이름과 프레임 워크에 대해 많은 오버로드가있어 throw가 확실합니다. 올바른 매개 변수 유형을 전달하여 정확한 오버로드를 지정함으로써 모호성을 해결할 수 있습니다. 'Type.GetMethod (string, Type [])'오버로드를 사용하십시오. – nawfal

답변

1

괜찮 냐고 물어 보지만 호출 식 구성이 잘못되었습니다. 실제 메서드 대신 메서드 정보 Invoke 메서드를 호출하는 식을 만들려고합니다. . 당신의 유형에

이 인스턴스에 메서드를 호출하는 식을 만들려면 다음과

var methodInfo = obj.GetType().GetMethod("HandleEventWithArg"); 

// Pass the instance of the object you want to call the method 
// on as the first argument (as an expression). 
// Then the methodinfo of the method you want to call. 
// And then the arguments. 
var body = Expression.Call(Expression.Constant(obj), methodInfo, argExpression); 

I've made a fiddle

PS를 : 나는 guessin 해요 g argExpression은 메서드에 예상되는 int이있는 식입니다.