F# 从ProvidedMethod访问实例

F# 从ProvidedMethod访问实例,f#,type-providers,F#,Type Providers,如何从aProvidedMethod 也就是说,如果foo是提供的类型,而擦除的类型是Bar,方法名为Execute 然后给出这个代码 foo.Execute() 我希望提供的方法与此相同 bar.Execute() //where bar is an object of type Bar 我有的是这个 //p is a MethodInfo that represents Bar.Execute and m is the ProvidedMethod objm.InvokeCode &

如何从a
ProvidedMethod

也就是说,如果
foo
是提供的类型,而擦除的类型是
Bar
,方法名为
Execute
然后给出这个代码

foo.Execute() 
我希望
提供的方法
与此相同

bar.Execute() //where bar is an object of type Bar
我有的是这个

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

实例作为
args
数组中的第一个表达式传递,因此您可以从那里访问它

另外,您需要构建的
InvokeCode
不应该捕获方法info
p
并使用反射对其调用
Invoke
。相反,您需要构建一个表示调用的F#引号(类似于表达式树)

所以,你需要这样写:

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) )

实例作为
args
数组中的第一个表达式传递,因此您可以从那里访问它

另外,您需要构建的
InvokeCode
不应该捕获方法info
p
并使用反射对其调用
Invoke
。相反,您需要构建一个表示调用的F#引号(类似于表达式树)

所以,你需要这样写:

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) )