有没有办法得到Calling function(最好的)或者function-I-am-in (第二个)?(为了设置对该函数的回调)我不希望每次调用时都指定要调用的函数的名称。请注意,这些函数是类方法。
这是我正在做的:(和不喜欢)
procedure TX.SetMeTimer(FAnimate:Tproc);
begin
Timer1.OnTimer:=FAnimate;
end;
procedure TX.Animate1;
begin
SetMeTimer(Animate1);
DoSomething;
end;
procedure TX.Animate2;
begin
SetMeTimer(Animate2);
DoSomething;
end;
// to TXAnimate500, until you are sick of copying the function name....
这就是我想要的样子:(调用函数的方法)
procedure TX.StartReEntrantTimer(FAnimate:Tproc);
begin
Timer1.OnTimer:=CallingFunction;
end;
procedure TX.Animate;
begin
SetMeTimer();
DoSomething;
end;
或函数-i-am-in方法
procedure TX.StartReEntrantTimer(FAnimate:Tproc);
begin
Timer1.OnTimer:=F;
end;
procedure TX.Animate;
begin
SetMeTimer(FunctionIamIn);
DoSomething;
end;
为什么?在对使用此工具的用户进行时间和动作研究时,输入错误的函数名称是一个重要的错误原因,我想删除它。
发布于 2017-10-15 18:50:08
Delphi附带的DUnit测试代码的单元TestFramework.pas有一个函数CallerAddr,它(在Delphi2005中)是这样实现的:
function CallerAddr: Pointer; {$IFNDEF CLR} assembler; {$ENDIF}
{$IFDEF CLR}
begin
Result := nil;
end;
{$ELSE}
const
CallerIP = $4;
asm
mov eax, ebp
call IsBadPointer
test eax,eax
jne @@Error
mov eax, [ebp].CallerIP
sub eax, 5 // 5 bytes for call
push eax
call IsBadPointer
test eax,eax
pop eax
je @@Finish
@@Error:
xor eax, eax
@@Finish:
end;
{$ENDIF}
(更新的版本已经修改,因此您应该检查您自己的Delphi版本的实现。)
用于在DUnit测试中调用方法的地址处引发异常。应该可以使用它,或者至少可以根据您的目的对其进行调整。
请注意,这可能只适用于Win32程序。
https://stackoverflow.com/questions/46749517
复制相似问题