Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 actionType = 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.

like image 534
Daniel Avatar asked Jul 26 '11 21:07

Daniel


1 Answers

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)

like image 107
Tomas Petricek Avatar answered Sep 26 '22 00:09

Tomas Petricek