Created
January 14, 2017 21:44
-
-
Save panesofglass/0a59892c0d96dce7aca1e60a243fd23f to your computer and use it in GitHub Desktop.
Define generic delegate type with F#
This file contains hidden or 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
| > defineGenericDelegate [| typeof<Collections.Generic.IDictionary<string, obj>>; typeof<Threading.Tasks.Task> |];; | |
| val it : Type = | |
| System.Func`2[System.Collections.Generic.IDictionary`2[System.String,System.Object],System.Thread | |
| ing.Tasks.Task] |
This file contains hidden or 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
| module Utils = | |
| open System | |
| let defineGenericDelegate (parameters:Type[]) = | |
| match parameters.Length with | |
| | 1 -> typeof<Func<_>>.GetGenericTypeDefinition().MakeGenericType(parameters) | |
| | 2 -> typeof<Func<_,_>>.GetGenericTypeDefinition().MakeGenericType(parameters) | |
| | 3 -> typeof<Func<_,_,_>>.GetGenericTypeDefinition().MakeGenericType(parameters) | |
| | 4 -> typeof<Func<_,_,_,_>>.GetGenericTypeDefinition().MakeGenericType(parameters) | |
| | 5 -> typeof<Func<_,_,_,_,_>>.GetGenericTypeDefinition().MakeGenericType(parameters) | |
| | 6 -> typeof<Func<_,_,_,_,_,_>>.GetGenericTypeDefinition().MakeGenericType(parameters) | |
| | 7 -> typeof<Func<_,_,_,_,_,_,_>>.GetGenericTypeDefinition().MakeGenericType(parameters) | |
| | 8 -> typeof<Func<_,_,_,_,_,_,_,_>>.GetGenericTypeDefinition().MakeGenericType(parameters) | |
| | 9 -> typeof<Func<_,_,_,_,_,_,_,_,_>>.GetGenericTypeDefinition().MakeGenericType(parameters) | |
| | 10 -> typeof<Func<_,_,_,_,_,_,_,_,_,_>>.GetGenericTypeDefinition().MakeGenericType(parameters) | |
| | _ -> failwith "Too many parameters" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment