Skip to content

Instantly share code, notes, and snippets.

@kekyo
Created May 22, 2016 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kekyo/8d26c5e95a7cfa8d73824a8721936d09 to your computer and use it in GitHub Desktop.
Save kekyo/8d26c5e95a7cfa8d73824a8721936d09 to your computer and use it in GitHub Desktop.
How to extract generic parameter type from runtime class definition. (F#)
open System
open System.Collections.Generic
let iedt = typedefof<IEnumerable<int>>
let traverseInterfaceType (targetType: Type) =
targetType.GetInterfaces()
|> Seq.filter (fun t -> t.IsGenericType && (t.GetGenericTypeDefinition() = iedt))
|> Seq.map (fun t -> t.GetGenericArguments().[0])
[<EntryPoint>]
let main argv =
let targetType = typeof<List<DateTime>>
let results = traverseInterfaceType targetType |> Seq.toArray
printfn "%A" results
0 // return an integer exit code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment