class Program
{
static void Main(string[] args)
{
Type t = typeof(A<,>);
Console.WriteLine(typeof(A<,>)); // prints A'2[T1,T2]
}
}
class A<T1,T2>
{
}
As far as I know, generic type A<T1, T2> is not an actual type, but rather a blueprint/template for an actual type, so why does typeof accept it as a parameter (since as far as I know, typeof accepts as parameter actual types)?
Thank you
In reflection the unconstructed generic type, say
C<>is conflated with the instance typeC<T>.This is perhaps less than theoretically pure; I think of these as very different entities. I think of one as the symbol “C with one type parameter” and the other as a compile time type
C<T>. In code,C<>andC<T>are not synonyms for each other; you can make a field of the latter type if T is in scope, but you cannot ever make a field of the former type.That the reflection library gives you the type of the instance type when you ask for the unconstructed type is not all bad though. What would you do with the unconstructed type? There’s nothing you really can do with it. But with the instance type, you can say “substitute int for T” in here.
The real benefit of getting
C<T>when you ask fortypeof(C<>)is that this always gives you the unconstructed type. Compare:When you call CT, what are you going to call it on? There’s no type
C<T>to call CT on. You can callC<int>.CTin which case you’ll get backC<int>, notC<T>. The only way to get the typeC<>constructed with T is to ask fortypeof(C<>).Does that make sense?
Reflection is simply the library of code that allows you to get information about your code in the code itself. “typeof” gives you a Type object which you can then “reflect” upon to find out information about the type.
The way I said it was confusing. I’ve rephrased it.