instaniate an instance "dynamically" at runtime

  • Thread starter Thread starter Action
  • Start date Start date
A

Action

Let's say
class parent
class B : parent
class C : parent
....etc. (may add later......so I don't know how many classes will there...)

I wanna to let the user type in the class name and instaniate a new instance
e.g.
string userinput = "classB"

parent temp = new SOMEFUNCTION("classB");
I wanted to ask what can SOMEFUNCTION be???



the point is that I dont' want to do
if (userinput == "classA")
temp = new A();
else if (userinput =="classB")
temp = new B();
....etc.

but rather
temp = new SOMEFUNCTION(userinput); // temp can be A or B or other...
 
Hi Action,

You can use reflection to create the objects.
Assembly.CreateInstance(string)

Anyway you have to know the name of the assembly where the type is defined
and this assembly has to be in memory already.

HTH
B\rgds
100
 
Back
Top