Arran549
Arran549

Reputation: 182

How do I create a Type with multiple generic type parameters

I have been searching of a while now, and have still not been able to find an answer to my question, instead I find answers to Making a Generic Method with multiple Generic Type parameters.

So, this is my problem. I have an interface IMyInterface<T1, T2>and I want to create it as a Type. Currently I can create the type for IMyInterface<T> like this :

Type type = typeof(IMyInterface<>).MakeGenericType(genericTypeForMyInterface) 

Any assistance would be greatly appreciated.

Thanks :)

Upvotes: 7

Views: 2426

Answers (1)

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73502

MakeGenericType takes params Type[] as arguments which will be used to construct a generic type. Which means that you could pass any number of arguments.

So you could simply do

Type type = typeof(IMyInterface<,>).MakeGenericType(type1, type2);

Note you need a comma (,) in between the angular brackets for types with multiple generic type parameters. You need n-1 comma(s) for (n) type parameters. i.e one comma for two type parameter, two commas for three type parameters an so on.

Upvotes: 16

Related Questions