I have the following scenario:
class Addition{
public Addition(int a){ a=5; }
public static int add(int a,int b) {return a+b; }
}
I am calling add in another class by:
string s="add";
typeof(Addition).GetMethod(s).Invoke(null, new object[] {10,12}) //this returns 22
I need a way similar to the above reflection statement to create a new object of type Addition using Addition(int a)
So I have string s= "Addition"
, I want to create a new object using reflection.
Is this possible?
Best Answer
I don't think
GetMethod
will do it, no - butGetConstructor
will.EDIT: Yes,
Activator.CreateInstance
will work too. UseGetConstructor
if you want to have more control over things, find out the parameter names etc.Activator.CreateInstance
is great if you just want to call the constructor though.