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 :)
A Generic class can have muliple type parameters.
You have to add the numbers as the same type, so you could do x. intValue() + y. intValue(); .
Multiple parameters You can also use more than one type parameter in generics in Java, you just need to pass specify another type parameter in the angle brackets separated by comma.
You cannot create instances of it unless you specify real types for its generic type parameters. To do this at run time, using reflection, requires the MakeGenericType method.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With