DELEGATE FOR ANY METHOD TYPE - C#

Goti Bandu

Total Post:119

Points:835
Posted by  Goti Bandu
C# 
C#
 1596  View(s)
Ratings:
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. AVADHESH PATEL

    Post:604

    Points:4228
    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. Vijay Shukla

    Post:100

    Points:700
    Re: Delegate for any method type - C#

    Hi!

    Try this way

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


Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!