Hello,
I've got a name of a method: "Garden.Plugins.Code.Beta.Business.CalculateRest"
How to run it? I think about this fancy reflection based solution like RunMethod(string MethodName)
Hello,
I've got a name of a method: "Garden.Plugins.Code.Beta.Business.CalculateRest"
How to run it? I think about this fancy reflection based solution like RunMethod(string MethodName)
It'll be slow, trust me. So don't put it in a critical place.
Other than that you'll just have to do it "by hand". Start enumerating through all the namespaces, classes, etc. until you find what you need. I don't think there is anything fancy pre-made that does this already. (Although I haven't searched)
BindingFlags.Public | BindingFlags.Static
.If you want to call an instance method or one which takes parameters, you'll need to work out how to get that information too.
If the type is an instance type:
Type.GetType("Garden.Plugins.Code.Beta.Business")
.GetMethod("CalculateRest").Invoke(myInstanceOfTheType, param1, param2);
If it's a static method:
Type.GetType("Garden.Plugins.Code.Beta.Business")
.GetMethod("CalculateRest").Invoke(null, param1, param2);
If it doesn't take parameters, just leave off "param1, param2, etc"...
It's not quite as simple as treating everything to the left of the last dot as the literal typename. If you've got a type of the form:
X.Y.Z.Type
then it's not necessarily the case that X, Y and Z are namespaces. They could also be types themselves and the subsequent parts could be inner classes:
class X
{
class Y
{
// etc
}
}
If this is the case then Type.GetType("X.YU") wont resolve to the Y class.Instead, the clr seperates inner classes with a + symbol, so you'd actually need to call Type.GetType("X+Y");
If the method that you're calling is a params method then you'll need to so some additional work. You're required to roll the variable parameters up into an array and pass this. You can check for variable parameters by grabbing the ParameterInfo data for a method and seeing if the last parameter has the ParamArray attribute attached.