Reputation: 13
I searched on the site for an answer that I needed, but I could not find it...
I have this:
string sNameOfClass="BusNode";
And the class already exists, and has its own properties.
Now I need to do something like this, but I dont know how...
sNameOfClass variable1 = new sNameOfClass()
and use varible1
forward in program as a normal variable...
so like
coorinateClass cs = new ks();
cs.a=11;
cs.b=33;
cs.c=55;
Any clues?
Thank you in advance
Upvotes: 0
Views: 115
Reputation: 14995
You can use Activator.CreateInstance
Method
Check out this link : Activator.CreateInstance Method (String, String)
Upvotes: 1
Reputation: 53
If you need to manage a class with its name you can use this:
//your class name
string sNameOfClass = "YourNameSpace.BusNode";
//create type class from your class name
Type T = Type.GetType(sNameOfClass);
//create new instance of class
var NewInstanse = Activator.CreateInstance(T);
//set property
T.GetProperty("a").SetValue(NewInstanse, 11);
//get value of property
var a = T.GetProperty("a").GetValue(NewInstanse);
Upvotes: 1
Reputation: 6079
string sNameOfClass="BusNode";
switch (sNameOfClass)
{
case "BusNode":
BusNode variable1 = new BusNode()
break;
case "...."
break;
}
Upvotes: 0