I am trying to make list of event handlers where handler is method reference. To delete specific handler i need to find it in the list. But how can i compare code address of two method references?
type
TEventHandler = reference to procedure;
procedure TestProc;
begin
end;
procedure TForm26.FormCreate(Sender: TObject);
var
Handlers: TList<TEventHandler>;
begin
Handlers := TList<TEventHandler>.create;
try
Handlers.Add(TestProc);
Handlers.Remove(TestProc); { doesn't work }
Assert(Handlers.Count=0); { fails }
Assert(Handlers.IndexOf(TestProc)>=0); { fails }
finally
FreeAndNil(Handlers);
end;
end;
Default comparer of TList<> doesn't compare method references properly. How can i compare them? Is there structure similar to TMethod but for method references?
TList<T>
does not useTEqualityComparer<T>
butTComparer<T>
but that does not matter as both fail because they just compare references (pointers). And the references passed toAdd
andRemove
are different as I explained in my answer. – Wobbly