In .NET there seem to be two ways to pass a type to a method or class. The first is through generics, in which we pass a type as a special parameter.
Such as:
var list = new List<MyClass>();
The other way is to explicity use the typeof operator such as:
var pe = Expression.ParameterExpression(typeof(MyClass), "myinstance");
My question is regarding the discrepancy in a uniform interface to methods that require a type parameter. Why can’t the above statement be done as follows?:
var pe = Expression.ParameterExpression<MyClass>("myinstance");
Is it because there are two semantic differences required in how the compiler behaves? When a generic parameter is processed by the compiler does it simply perform substitution ala lambda calculus? Whereas the typeof style methods require an actual instance of the Type class to infer attributes and properties from?
Thank you.
The first method allows you to calculate the required type at runtime.
Personally I wouldn’t mind seeing an overload which would definitely make code with a type defined at compile time much cleaner.