Skip to content

Instantly share code, notes, and snippets.

@panesofglass
Created January 14, 2017 21:44
Show Gist options
  • Select an option

  • Save panesofglass/0a59892c0d96dce7aca1e60a243fd23f to your computer and use it in GitHub Desktop.

Select an option

Save panesofglass/0a59892c0d96dce7aca1e60a243fd23f to your computer and use it in GitHub Desktop.
Define generic delegate type with F#
> 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]
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