4
Reply

Type as a variable

John

John

Dec 18 2009 9:12 AM
2.6k

Does anyone know how to make this type of code work?
Type t = typeof(int);  // can also be a user designed type
var x= new t();  // won't work!
Same sort of issue: given a MethodInfo, "mi" invoked on an object, the resultant is of type object and must be cast. This works:
int n = (int) mi.Invoke(someobject, null);  n has correct value
This does not work:
Type t = typeof(int);
var n = (t) mi.Invoke(someobject, null);
Any idea on how to do this? It works in Delphi but I cannot port the code to C#!
Thanks!
 

Answers (4)