Can't check the equality of different delegate objects for the same function on Android/iOS

public class TestClass {

    public delegate void MyDelegate();

    public static void doTest() {

        MyDelegate a = doSth;
        MyDelegate b = doSth;
        object objA = a;
        object objB = b;

        // Case 1
        bool isEqual = a == b;
        /**
        .NET: true
        Android/iOS: false
        */

        // Case 2
        #if COOPER
        bool isObjEqual = objA.equals(objB);
        #elif ECHOES
        bool isObjEqual = objA.Equals(objB);
        #elif NOUGAT
        bool isObjEqual = objA.isEqual(objB);
        #endif
        /**
        .NET: true
        Android/iOS: false
        */
    }

    public static void doSth() {}
}

This issue will make normal code as below can’t work as expected:

    private void eventHandler(){
    }

    public void testEvent(){
        var ev = new Event();
        ev.addListener(eventHandler);
        ev.removeListener(eventHandler);    // will fail to remove listener on Android/iOS
    }

The cause of the issue is obvious: everytime we try to pass a function as a delegate parameter, it will generate a new delegate object for it. DotNet’s Delegate class has overridden the Equals method so it can solve the problem, but it seems not be the best choice to solve it for Android/iOS.

The best solution is to only generate one delegate object for one function if needed, but I’m not sure if it’s easy to implement.

@ck could you help to check this?

I was hoping I could think of a solution, but other than keeping the delegate around I don’t see a good one. Cocoa delegates can’t implement a compare operator as far as I can tell.

Actually the compare operator is not so important. If there’s a way to determine if MyDelegate a and b are pointing to the same member function doSth on Android/iOS, I can wrap it as a helper method to solve it. Is this possible?

Right but this is kind of the same problem as we had before with figuring out the ‘target’ for a delegate. We’d need to somehow store “hey, this isn’t an anonymous really, it’s a method”. I’m trying to figure out how to best implement that.