I am trying to cast a WaitCallBack into an Action<object>
I am doing this by:
WaitCallBack w1 = Foo;
Action<object> a1 = new Action<object>(w1);
Action<object> a2 = Foo;
However, when i try to compare the a1 with a2, it will return false.
Is there away to cast the delegates, so that the equality will be true?
Thanks.
Delegates compare by method+target; the problem is that you are comparing different things!
Perhaps it becomes clearer if we expand this line to what the compiler sees:
is actually:
(using the implicit
Invokeoperation on a delegate)You you can see that the target is
w1, and the method isInvoke. We can confirm this:Compare to the other:
Here, the target is either
null(if static) orthis(if non-static), and the method isFoo. The delegates are not the same. It is correct that it reportsfalse.You can check this chained operation manually, but it is a bit tedious, especially if you need to consider every combination/depth; but a trivial example: