The below code won't work, I wanted to know how I can dynamically cast an instance to a type determined at runtime?
Convert.ChangeType() returns an Object that still needs to be cast. So does all attempts to Invoke() a GetConstructor() or Activator.CreateInstance(), see below. At some point I need to explicitly cast in code, I'm hoping to avoid it or push it out as far as possible.
Type type = Type.GetType ("RandomChildClass");
Object obj = Activator.CreateInstance (type, new Object[]{ "argument" });
var instance = (type)obj;
I know I can create a method to accept < T >, but I'd still have the same problem of not knowing how to call it with a dynamic random type Casting a variable using a Type variable
instance
after you cast? – Osteal