问题描述
如何使用反射创建一个 Action
的实例?这是我有的:
How would I create an instance of Action<'T>
using reflection? Here's what I have:
let makeAction (typ:Type) (f:'T -> unit) =
let actionType = typedefof<Action<_>>.MakeGenericType(typ)
let converter = FSharpFunc.ToConverter(f)
Delegate.CreateDelegate(actionType, converter.Method)
哪些barfs与:
'T
是一个接口, typ
implements。
'T
is an interface, which typ
implements.
推荐答案
我认为有两个问题。第一个是你需要调用 CreateDelegate
重载,它需要三个参数。附加参数指定应该调用该方法的实例。
I think there are two problems. The first one is that you need to call CreateDelegate
overload that takes three arguments. The additional argument specifies the instance on which the method should be invoked.
第二个问题是 Converter<'T,unit> code>实际编译为返回
Microsoft.FSharp.Core.Unit
的方法,而不是返回 void
。我不确定是否有更容易的解决方法,但您可以定义一个包含方法的包装器。会员被编译成C#,所以在这种情况下,单元类型将被编译为 void
:
The second problem is that the Converter<'T, unit>
actually compiles as a method that returns Microsoft.FSharp.Core.Unit
and not a method that returns void
. I'm not sure if there is an easier workaround, but you can define a wrapper that has a method. Members are compiled to look like C#, so the unit type will be compiled as void
in that case:
open System
type Wrapper<'T>(f:'T -> unit) =
member x.Invoke(a:'T) = f a
let makeAction (typ:Type) (f:'T -> unit) =
let actionType = typedefof<Action<_>>.MakeGenericType(typ)
let wrapperType = typedefof<Wrapper<_>>.MakeGenericType(typ)
let wrapped = Wrapper<_>(f)
Delegate.CreateDelegate(actionType, wrapped, wrapped.GetType().GetMethod("Invoke"))
makeAction (typeof<int>) (printfn "%d")
编辑 - 稍微改动,使其在您的场景中实际工作(使用界面)
EDIT - Did a minor change to make it actually work in your scenario (with interface)
这篇关于创建一个Action<'T>的实例使用反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!