2

I would like to know if there is a way in c# to execute a method, if we have only it's name in a string ?

string methodname="method"
this.RunMethod(methodname)

Something like that ?

Thanks :)

Thoma Biguères
  • 1,136
  • 4
  • 18
  • 42
  • and the keyword to search is 'Reflection' – Iarek Apr 26 '12 at 13:37
  • @AdrianIftode: the type would be `this.GetType()` – spender Apr 26 '12 at 13:37
  • 1
    yes you can, but I would caution you not to. Reflection is a very powerful tool, but isn't meant for everyday use. Typically you only need this when building dynamic infrastructure/frameworks. but for everyday business solutions encapsulating change using common design patterns is enough to solve the problem. – Jason Meckley Apr 26 '12 at 13:41

7 Answers7

2

Try this:

Type t = Type.GetType(typeName);
t.InvokeMember("method", BindingFlags.InvokeMethod | BindingFlags.Public, null, null, null);
ionden
  • 12,536
  • 1
  • 45
  • 37
0

Something like

Type type = GetType("MyType");
MethodInfo methodInfo = type.GetMethod(methodName);
methodInfo.Invoke()
Dave Hogan
  • 3,201
  • 6
  • 29
  • 54
0

I used seomthing like this:

typeof(StringValidator<T>).GetMethod(restriction.Operacion.Nombre).Invoke(null, param) as IList<T>;

based on the type of the class that contains the method (StringValidator) then I get the method by it's name restriction.Operacion.Nombre, and Invoke it.

hope it helps

Eduardo Crimi
  • 1,551
  • 13
  • 13
0

This example has what you are looking for namely:

String s = (String)calledType.InvokeMember(
                methodName,
                BindingFlags.InvokeMethod | BindingFlags.Public | 
                    BindingFlags.Static,
                null,
                null,
                null);

(Obviously this may change depending on return types etc.)

NominSim
  • 8,447
  • 3
  • 28
  • 38
0

You need the type, the method name, and an instance if the method is not static

Check Reflection: How to Invoke Method with parameters

Community
  • 1
  • 1
jorgehmv
  • 3,633
  • 3
  • 25
  • 39
0

You can create a generic method to invoke methods by their name on any instance you want this way:

public void RunMethodOn(object obj, String methodName,params Object[] args)
        {
            if(obj != null)
            {
                System.Reflection.MethodInfo method = null;
                method = obj.GetType().GetMethod(methodName);
                if (method != null)
                    method.Invoke(obj, args);
            }
        }

And the use it to call a method on your own instance as you ask:

public void RunMethod(String methodName,params Object[] args)
            {
                   ExecuteOn(this, methodName, args);
            }
Mg.
  • 1,469
  • 2
  • 16
  • 29
0

You can do this very nicely with an extension method:

public static class MethodEx
{
    public static void RunMethod(
         this object target,
         string methodName,
         params object[] values)
    {
        var type=target.GetType();
        var mi = 
          type.GetMethod(methodName,values.Select(v=>v.GetType()).ToArray());
        mi.Invoke(target,values);
    }
}

...and use it as follows:

void Main()
{
    var m=new Monkey();
    m.RunMethod("A",1);
    m.RunMethod("A","frog");
}

class Monkey
{
    public void A(int a)
    {
        Console.WriteLine("int A called");
    }
    public void A(string s)
    {
        Console.WriteLine("string A called");
    }
}

which will choose the right overload according to the supplied params, so the code above gives output:

int A called

string A called

I leave better error handling to the OP.

spender
  • 117,338
  • 33
  • 229
  • 351