开发者

How can I get a method handler by its name? (c#)

开发者 https://www.devze.com 2023-02-13 06:16 出处:网络
Please look on the code below to understand my problem: public class MyClass { public delegate object MyDelegate(object value);

Please look on the code below to understand my problem:

public class MyClass
{
  public delegate object MyDelegate(object value); 

  public MyDelegate GetMethodByName(string methodName)
  {
     // What have to be here?
  }

  public object Method1(object value)
  {
     // some code here
  }

  public object Method2(object value)
  {
     // some code here
  }

  public object Method3(object value)
  {
     // some code here
  }
}

And somewhere:

var obj = new MyClass();
MyDelegate del = obj.GetMethodByName("Method1");
var result = del(someobject);

S开发者_开发知识库o how can I get a method handler by its name? (c#)


var obj = new MyClass();
MyDelegate del = (MyDelegate)Delegate.CreateDelegate(typeof(MyDelegate), obj.GetType().GetMethod("Method1"));
var result = del(someobject);


public class MyClass
{
    public delegate object MyDelegate(object value);

    public MyDelegate GetMethodByName(string methodName)
    {
        return (MyDelegate)Delegate.CreateDelegate(typeof(MyDelegate), this.GetType().GetMethod(methodName));
    }

    public object Method1(object value)
    {
        throw new NotImplementedException();
    }

    public object Method2(object value)
    {
        throw new NotImplementedException();
    }

    public object Method3(object value)
    {
        throw new NotImplementedException();
    }
}


I think what you are trying to achieve here is dynamic method invocation from within C#. There are a lot of ways of getting this done to be honest. Most people would use reflection for this kind of thing but I woudl rather re-engineer the code a bit. Here is a link that might help

http://www.csharphelp.com/2006/05/dynamic-method-invocation-in-c-using-reflection/


It appears that you need to dynamically construct a delegate to a method that is retrieved by reflection. To do this, you can use the CreateDelegate method as follows.

public MyDelegate GetMethodByName(string methodName)
{
    MethodInfo method = GetType().GetMethod(methodName);  // use BindingFlags if method is static/non-public.
    return (MyDelegate)Delegate.CreateDelegate(typeof(MyDelegate), method);
}

Of course, you need to make sure that the signature of MyDelegate matches that of the given method.


 public class MyClass
{
    public delegate object MyDelegate(object value);

    MyDelegate del1, del2, del3;

    public MyClass()
    {
        del1 = Method1;
        del2 = Method2;
        del3 = Method3;
        // remaining Ctr code here

    }

    public MyDelegate GetMethodByName(string methodName)
    {
        if (methodName.Equals("Method1"))
            return del1;

        if (methodName.Equals("Method2"))
            return del2;

        if (methodName.Equals("Method3"))
            return del3;

        return null;

    }

    public object Method1(object value)
    {
        // some code here
        return null;
    }

    public object Method2(object value)
    {
        // some code here
        return null;
    }

    public object Method3(object value)
    {
        // some code here
        return null;
    }
}

If your list of methods you want to lookup from is limited to your statically defined methods , and you dont have overloaded methods , then this solution works without the overhead of using reflection. However , if you want the solution to be generic , or work with overloaded methods , then you would go the way other posts have mentioned using reflection.


Using reflection, you could get a reference to the MethodInfo instance like this.

    MethodInfo[] methodInfos = typeof(MyClass).GetMethods(BindingFlags.Public |
                                              BindingFlags.Static);
    MethodInfo method1 = methodInfos.SingleOrDefault(m => m.Name == "method1");
0

精彩评论

暂无评论...
验证码 换一张
取 消