I’d like to create a new instance of a controller, via a string name. In classic ASP I’d do an eval, but how to do it in c#?
eg:
If I wanted to create an instance of the “AccountController, normally I’d write:
var Acc = new AccountController();
Now if the controller name is only available as a string, how could I instantiate the object?
eg,
var controllerName = "AccountController";
var acc = new eval(controllerName)();
Thank you for any help!
Frank
If your controller is within the assembly you just need a class name, otherwise you need a fully qualified name (namespace and assembly). You would do it using reflection. Something like:
There are other ways, but all involve reflection (
System.Reflection).