A method reference can be broken down in to two parts, the pointer to the object and the pointer to the method itself. There is a convenient record type defined in the System
unit called TMethod
that allows us to do that break down.
With that knowledge, we can write something like this:
function SameMethod(AMethod1, AMethod2: TNotifyEvent): boolean;
begin
result := (TMethod(AMethod1).Code = TMethod(AMethod2).Code)
and (TMethod(AMethod1).Data = TMethod(AMethod2).Data);
end;
Hope this helps. :)
Edit: Just to lay out in a better format the problem I am trying to solve here (as alluded to in the comments).
If you have two forms, both instantiated from the same base class:
Form1 := TMyForm.Create(nil);
Form2 := TMyForm.Create(nil);
and you assign the same method from those forms to the two buttons:
Button1.OnClick := Form1.ButtonClick;
Button2.OnClick := Form2.ButtonClick;
And compare the two OnClick
properties, you will find that the Code
is the same, but the Data
is different. That is because it's the same method, but on two different instantiations of the class...
Now, if you had two methods on the same object:
Form1 := TMyForm.Create(nil);
Button1.OnClick := Form1.ButtonClick1;
Button2.OnClick := Form1.ButtonClick2;
Then their Data
will be the same, but their Code
will be different.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…