1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
|
let invokeUntyped (value:obj) =
// Step 1: get the underlying System.Type for the value
let t = value.GetType()
// Step 2: locate the required method and apply the type argument
let methodInfo =
typeof<MyApi>
.GetMethod("MyMethod", BindingFlags.Public ||| BindingFlags.Static)
.MakeGenericMethod [|t|]
// Step 3: since the parameter is optional, it must be wrapped
let optTy = typedefof<_ option>.MakeGenericType [|t|]
let optCtor = optTy.GetConstructor [|t|]
let optVal = optCtor.Invoke [|value|]
/// Step 4: invoke the method with constructed optional parameter
methodInfo.Invoke(null, [|optVal|])
|