If we have something like this:
interface ISomething<U,V> { ... } class Something<U,V> : ISomething<U,V> { ... }
typeof(ISomething<,>) and typeof(Something<,>) will lead to a definition of "generic type". But if we move on to the type of interface as an interface implemented by the class, it will be a constructed type that is not actually associated with any of its type parameters:
typeof(Something<,>).GetInterfaces().SingleOrDefault()
MSDN specifically mentions this. I want to build the same type (built type) ISomething<,> directly (without subclassing and searching for the base type), and I could not find a way to do this.
Additional Information:
I even tried this:
Type t1 = typeof(ISomething<,>); Type t2 = t1.MakeGenericType(t1.GetGenericArguments())
In the above code:
t1.Equals(t2) true, but t1.Equals(t3) is false, obviously because t3 built.
Surprisingly, t1.GetGenericArguments()[0].Equals(t3.GetGenericArguments()[0]) is false, although both are open (IsGenericParameter = true), and I could not find the difference in their properties.
And this is why I need to do this: I need a canonical form for storing type objects in a list. Objects sometimes come from base classes / interfaces (e.g. t3 above), and sometimes directly (e.g. t1). I will need to be able to compare them with each other. I cannot save the definition of the generic type (using .GetGenericTypeDefinition() ), because sometimes I will have a partially open constructed generic type (e.g. ISomething), and GetGenericTypeDefinition will give me the type without any type arguments.
The only way I can make canonical types that I thought might work is to check if all the arguments are of the type, and do GetGenericTypeDefinition. Otherwise, save the constructed type.