0

I have a method which takes method delegate having parameter

 public delegate void RunOperation(object o );
  public void Abort(RunOperation operationToRun, object obj)
  {
  }
  public void AllMessages()
  {
  }

Is it possible to pass AllMessage() as a delegate to Abort() ?

I don't want to create any new delgate for parameterless methods.

Thanks

BreakHead
  • 10,480
  • 36
  • 112
  • 165

2 Answers2

2

No and Yes.

You can like this:

Abort(_ => AllMessages(), null);

But you're just creating another method that calls AllMessages and doesn't use the object parameter.

Ray
  • 45,695
  • 27
  • 126
  • 169
0

I would solve it in this way:

public void Abort(Action operationToRun, object obj)
{

}
public void Abort(Action<object> operationToRun, object obj)
{

}
public void AllMessages()
{

} 

Explanation:

Make an overload for the Abort method. First method takes an action without parameter and second takes an action with one parameter. So you can call Abort with a parameterless method and a method with an object parameter.

Fischermaen
  • 12,238
  • 2
  • 39
  • 56