Cyan Cyan - 3 months ago 16
C# Question

Get the actual type from a Type variable

I am trying to get a type from a Type variable. For example:

Type t = typeof(String);
var result = SomeGenericMethod<t>();


An error happens on the second line, because
t
is not a
type
, it's a variable. Any way to make it a type?

Answer

No, you cannot know the value of a Type object at compile time, which is what you would need to do in order to use a Type object as an actual type. Whatever you're doing that needs to use that Type will need to do so dynamically, and not require having a type known at compile time.