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.
I think there are two problems. The first one is that you need to call
CreateDelegateoverload 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 returnsMicrosoft.FSharp.Core.Unitand not a method that returnsvoid. 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 asvoidin that case:EDIT – Did a minor change to make it actually work in your scenario (with interface)