C #: Can I pass an explicit delegate to an Action delegate?

Given:

delegate void Explicit(); 

Can I:

 public void Test(Explicit d) { Action a; a = d; // ???? } 

I have a scenario where I need to overload a constructor that has:

 public MyClass(Expression<Action> a) {} 

but the following overload is ambiguous:

 public MyClass(Action a) {} 

I decided that using an explicit delegate would resolve the ambiguity, but I need to pass that explicit delegate to the action in order to use the existing code.

+4
source share
3 answers
 Action a = new Action(d); 
+11
source

No, you cannot distinguish between different types of delegates with matching signatures with each other. You must create a new delegate / lambda expression of the target type and go back to the original.

+8
source

You can also specify the Invoke method to create a new Action delegate.

 Action a = new Action(d.Invoke); 
+4
source

All Articles