Created
May 22, 2016 13:48
-
-
Save kekyo/8d26c5e95a7cfa8d73824a8721936d09 to your computer and use it in GitHub Desktop.
How to extract generic parameter type from runtime class definition. (F#)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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