Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically calling method and class name

Tags:

c#

.net

class

I have some cases where I have to call method names from class names.

string scenario1 = "MockScenario1";
string scenario2 = "MockScenario2";

MockScenario1.GetInfo();
MockScenario2.GetInfo();

How can I dynamically use the strings to call method name here like

scenario1.GetInfo()
scenario2.GetInfo()

I tried to find out all options by string and control space to find related options. Any suggestions?

I am tried the below and trying to Get class name generated dynamically

The below code generated method name dynamically

string methodName = "hello";

//Get the method information using the method info class
 MethodInfo mi = this.GetType().GetMethod(methodName);

//Invoke the method
// (null- no parameter for the method call
// or you can pass the array of parameters...)
mi.Invoke(this, null);

More clear scenario: I am trying to send class name as parameter MockScenario1 and MockScenario2 are class names.

string scenarioCats = "MockScenario1";
string scenarioDogs = "MockScenario2";
GetResult(scenarioCats);
GetResult(scenarioDogs);

public static void GetCatsResult(string scenarioCats){
scenarioCats obj = new scenarioCats();
    obj.GetInfo();    
}
public static void GetDogsResult(string scenarioDogs){
scenarioDogs obj = new scenarioDogs();
    obj.GetInfo();    
}
like image 207
Kurkula Avatar asked Oct 25 '16 14:10

Kurkula


1 Answers

How to create an instance of a type from its string representation:

string scenario1 = "TheNamespace.MockScenario1";
Type theType = this.GetType().Assembly.GetType(scenario1);
var theInstance = (MockScenario1)Activator.CreateInstance(theType);
theInstance.GetInfo();

It will be better if your classes implement a common interface, for example IGetInfoAware, and then you could write a more generic loader:

var theInstance = (IGetInfoAware)Activator.CreateInstance(theType);

Note: you need to provide the full class name for scenario1 and scenario2

See Activator.CreateInstance

EDIT:

As @Georg pointed out, if the type is not declared in the assembly of the context objects, then it is necessary first to get the assembly where the type is hosted:

var theAssembly = (
    from Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()
    where (assembly.FullName == "TheNamespace.AssemblyName")
    select assembly
)
.FirstOrDefault();

if ( theAssembly!= null ){
    Type theType = theAssembly.GetType(scenario1);
    var theInstance = (IGetInfoAware)Activator.CreateInstance(theType);
    theInstance.GetInfo();
}

If for some reason the assembly name is unknown to you, then the type could be resolved like the following:

public Type GetTypeFromString(String typeName)
{
    foreach (Assembly theAssembly in AppDomain.CurrentDomain.GetAssemblies())
    {
        Type theType = theAssembly.GetType(typeName);
        if (theType != null)
        {
            return theType;                    
        }
    }
    return null;
}
like image 93
E-Bat Avatar answered Oct 20 '22 00:10

E-Bat