I'd like to get MethodInfo for caller-provided functions.
module MethodInfo =
let from0 (f: unit -> 'a) = (Func<'a>(f)).Method
let from1 (f: 'a -> 'b) = (Func<'a,'b>(f)).Method
let mi = MethodInfo.from0 (fun () -> 5)
mi.Invoke(null, Array.empty<obj>)
This compiles, but always throws "System.Reflection.TargetException : Non-static method requires a target."
The exception is not thrown if I pass an anonymous function directly to a method.
type MethodInfo =
static member from0 (f:Func<'a>) = f.Method
static member from1 (f:Func<'a,'b>) = f.Method
MethodInfo.from0 (fun () -> 5)
mi.Invoke(null, Array.empty<obj>)
Why does the conversion from F# functions to Func cause this exception? Is there a better way to get MethodInfo (some update since this old thread)?