Home > DeveloperSection > Forums > Delegate for any method type - C#
Goti Bandu

Total Post:119

Points:835
Posted on    April-06-2013 2:04 AM

 C# C# 
Ratings:


 2 Reply(s)
 1280  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?


AVADHESH PATEL

Total Post:604

Points:4228
Posted on    April-06-2013 9:07 AM

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));


Vijay Shukla

Total Post:100

Points:700
Posted on    April-06-2013 9:24 AM

Hi!

Try this way

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



Don't want to miss updates? Please click the below button!

Follow MindStick