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.