Advertise with us

Delegate for any method type - C#

Posted by  Goti Bandu
 2203  View(s)
Rate this:
Hi Everyone!

I want to have a class that will execute any external method, like this:

class CrazyClass
  //other stuff

  public AnyReturnType Execute(AnyKindOfMethod Method, 
    object[] ParametersForMethod)
    //more stuff
    return Method(Parameters) //or something like that

Is this possible? Is there a delegate that takes any method signature?
  1. Re: Delegate for any method type - C#

    Hi Goti!

    tTy different way by Func<T> and closures:

    public T Execute<T>(Func<T> method)
       // stuff
       return method();
    The caller can then use closures to implement it:

    var result = yourClassInstance.Execute(() => SomeMethod(arg1, arg2, arg3));

  1. Re: Delegate for any method type - C#


    Try this way

    public object Execute(MethodInfo mi, object instance = null, object[] parameters = null)
        return mi.Invoke(instance, parameters);

      Modified On Mar-29-2018 11:43:24 PM