I’m having the following classes –
public class A
{
public class ChildClass
{
public string name;
public string GetValue()
{
}
}
}
public Class B
{
string className = "ChildClass";
//I want to create an object of ChildClass here
//and call the GetValue() method
}
How can I instantiate ChildClass in B and access its members with the class name I’ve?
Updated Code –
namespace LoadObjectByName
{
class Program
{
static void Main(string[] args)
{
B obj = new B();
obj.GetVal();
}
}
public class A
{
public class ChildClass
{
public string name;
public string GetValue()
{
return "Invoked!";
}
}
}
public class B
{
public string className = "ChildClass";
public dynamic instance = Activator.CreateInstance(Type.GetType("A.ChildClass"));
public dynamic GetVal()
{
return instance.GetValue();
}
}
}
Something like this:
Or: