Class name passning

  • Thread starter Thread starter web1110
  • Start date Start date
W

web1110

I have a situation where I have a central control process for maintaining
objects.

Rather than instantiate an object and pass it to the controler object, I
would like to pass the class (unistantiated) to the controller and let the
controller do the instantiation. Is this possible?

To clarify with pseudo code

This is fine:
class ClassX...

X = new ClassX();
ClassManager.HeresTheClassToManage(X);

It would be nice to do this:

class ClassX...
ClassManager.InstantiateAndManageThisClass (ClassX);
 
web1110,

Just pass the type to your InstantiateAndManageThisClass method, like
so:

// Create the class.
ClassManager.InstantiateAndManageThisClass(typeof(ClassX));

Then, in your InstantiateAndManageThisClass method, call the static
CreateInstance method on the Activator class, passing the type. It will
construct the class for you.

You just have to make sure that each of these types implements an
interface or derives from a base class that you have the definition of.
That way, you can call methods and access properties on the instance
(otherwise, you only have object, and you would have to use reflection).

Hope this helps.
 
Why would you want to do this? What are you actually trying to accomplish?
 
Back
Top