2014-01-12 2 views
0

나는 foo가 제공하는 유형이며, 삭제 유형 Execute 라는 방법 Bar 경우있는 상태에서 ProvidedMethodProvidedMethod에서 인스턴스 액세스

의 구현에서 (실행) 인스턴스에 액세스하려면 어떻게 그럼이

bar.Execute() //where bar is an object of type Bar 
동일하기 ProvidedMethod 소원이 코드

foo.Execute() 

을 부여 16,

는 내가 가지고있는 것은 인스턴스가 args 배열의 첫 번째 표현으로 전달이

//p is a MethodInfo that represents Bar.Execute and m is the ProvidedMethod 
objm.InvokeCode <- fun args -> <@@ p.Invoke(--what goes here--,null) @@> 

답변

4

, 그래서 당신은 거기에서 액세스 할 수 있습니다.

또한 빌드해야하는 InvokeCode은 메소드 정보 p을 캡처해서 리플렉션을 사용하여 Invoke으로 호출해서는 안됩니다. 대신, 이 호출을 나타내는을 나타내는 F # 인용문 (표현식 트리와 유사 함)을 작성해야합니다. 이미 통화 할 방법에 대한 MethodInfo을 가지고 있기 때문에 내가 여기 <@@ .. @@>을 사용하지 않은

objm.InvokeCode <- (fun args -> 
    // The 'args' parameter represents expressions that give us access to the 
    // instance on which the method is invoked and other parameters (if there are more) 
    let instance = args.[0] 
    // Now we can return quotation representing a call to MethodInfo 'p' with 'instance' 
    Expr.Call(instance, p)) 

참고 :

그래서, 당신은 이런 식으로 뭔가를 작성해야합니다. 대신 ProviderRuntime.DoStuff(instance) 메서드를 호출하려면 인용 리터럴을 사용할 수 있습니다.

objm.InvokeCode <- (fun args -> 
    let instance = args.[0] 
    <@@ ProviderRuntime.DoStuff(%%instance) @@>)