Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delegate for any method type - C#

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(ParametersForMethod) //or something like that
  }
}

Is this possible? Is there a delegate that takes any method signature?

like image 864
Daniel Möller Avatar asked Apr 02 '13 18:04

Daniel Möller


People also ask

What is a delegate type C#?

A delegate is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance.

How do you write a delegate method in C#?

In C# 3.0 and later, delegates can also be declared and instantiated by using a lambda expression, as shown in the following example. // Instantiate Del by using a lambda expression. Del del4 = name => { Console. WriteLine($"Notification received for: {name}"); };

What is a delegate method?

A delegate method is a method that the delegate object is expected to implement. Some delegate methods are required, while some are not. In IOS, most delegates are expected to conform to an Objective-C protocol; the protocol declaration will tell you which methods are optional and which are required.

How many methods can be called using a delegate?

Only one method can be called using a delegate.

What is a method delegate in C++?

In simple words, it is a type that represents references to methods with a particular parameter list and return type and then calls the method in a program for execution when it is needed. Provides a good way to encapsulate the methods. Delegates are the library class in System namespace. These are the type-safe pointer of any method.

Can a delegate refer to any type of object?

A delegate has no knowledge of the instance type aside from the method it wraps, so a delegate can refer to any type of object as long as there is a method on that object that matches the delegate signature. When a delegate is constructed to wrap a static method, it only references the method.

Can a method have the same return type as a delegate?

But in the context of delegates, the signature does include the return value. In other words, a method must have the same return type as the delegate. This ability to refer to a method as a parameter makes delegates ideal for defining callback methods.

How do you reference a delegate in Java?

A delegate can refer to a method, which has the same signature as that of the delegate. The preceding delegate can be used to reference any method that has a single string parameter and returns an int type variable. Once a delegate type is declared, a delegate object must be created with the new keyword and be associated with a particular method.


1 Answers

You can do this a 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));

The advantage here is that you allow the compiler to do the hard work for you, and the method calls and return value are all type safe, provide intellisense, etc.

like image 77
Reed Copsey Avatar answered Sep 21 '22 12:09

Reed Copsey