开发者

Create an instance of Action<'T> using reflection

How would I create an instance of Action<'T> using reflection? Here's what I have:

let makeAction (typ:Type) (f:'T -> unit) = 
  let act开发者_运维技巧ionType = typedefof<Action<_>>.MakeGenericType(typ)
  let converter = FSharpFunc.ToConverter(f)
  Delegate.CreateDelegate(actionType, converter.Method)

which barfs with:

System.ArgumentException: Error binding to target method.

at System.Delegate.CreateDelegate(Type type, MethodInfo method, Boolean throwOnBindFailure)

'T is an interface, which typ implements.


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.

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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜