Mikael Lundin
Mikael Lundin

Reputation: 289

Calling generic function with instance of type

I've struggled with this problem for quite some time now, and can't seem to find any solution. Let me simplify it for you.

I have a generic function that I want to call, but the type argument I want to call it with only as an instance. Example

let foo_a<'a> () = typeof<'a>
let foo_b (t : System.Type) = foo_a<t>() // of course this does not work

I would like the following statement to be true

foo_a<int>() = foo_b(typeof<int>)

In C# I would've reflected out foo_a's MethodInfo and do MakeGenericMethod(t), but how do I do this in F#?

Just to clearify, flipping the dependency and making foo_a calling foo_b instead, is not an option for me.

Upvotes: 5

Views: 1183

Answers (1)

Jack P.
Jack P.

Reputation: 11525

As @svick said, there's no special way to do this in F# -- you need to use Reflection just like you would in C#.

Here's a simple example you can paste into F# interactive:

open System.Reflection

type Blah =
    //
    static member Foo<'T> () =
        let argType = typeof<'T>
        printfn "You called Foo with the type parameter: %s" argType.FullName


let callFoo (ty : System.Type) =
    let genericFoo =
        typeof<Blah>.GetMethod "Foo"

    let concreteFoo =
        genericFoo.MakeGenericMethod [| ty |]

    concreteFoo.Invoke (null, Array.empty);;  // The ;; is only needed for F# interactive

Output:

> callFoo typeof<int>;;
You called Foo with the type parameter: System.Int32
val it : obj = null

Upvotes: 3

Related Questions