F# 从ProvidedMethod访问实例
如何从aF# 从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 &
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
不应该捕获方法infop
并使用反射对其调用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
不应该捕获方法infop
并使用反射对其调用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) )