Using typeof as a generic type argument

According to typeof documentation :

[...] To get the runtime type of an expression, you can use the .NET Framework method GetType

Therefore, this means that it typeofshould be an expression of compilation time.

The article on Type Type Parameters states that:

[...] The type argument for this particular class can be any type recognized by the compiler.

Any type recognized by the compiler is any type deduced by the compiler, that is, any type that is known at compile time.

If so, why is the following statement not allowed?

int value = GenericMethod<typeof(int)>();
+4
source share
1 answer

typeof() Type, .GetType(), . , , <T> , :

T value = GenericMethod<T>();
+3

All Articles