如何在Delphi中获取当前过程/函数的名称(作为字符串) [英] How To Get the Name of the Current Procedure/Function in Delphi (As a String)

查看:645
本文介绍了如何在Delphi中获取当前过程/函数的名称(作为字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在过程/函数中以字符串形式获取当前过程/函数的名称?我想会有一些在编译时扩展的宏".

Is it possible to obtain the name of the current procedure/function as a string, within a procedure/function? I suppose there would be some "macro" that is expanded at compile-time.

我的情况是这样的:我有很多提供了记录的过程,它们都需要从检查记录的有效性开始,因此它们会将记录传递给验证程序".如果记录无效,则验证器过程(所有过程都相同)会引发异常,并且我希望异常消息不包括验证器过程的名称,而是包括调用验证器的函数/过程的名称程序(自然).

My scenario is this: I have a lot of procedures that are given a record and they all need to start by checking the validity of the record, and so they pass the record to a "validator procedure". The validator procedure (the same one for all procedures) raises an exception if the record is invalid, and I want the message of the exception to include not the name of the validator procedure, but the name of the function/procedure that called the validator procedure (naturally).

也就是说,我有

procedure ValidateStruct(const Struct: TMyStruct; const Sender: string);
begin
 if <StructIsInvalid> then
    raise Exception.Create(Sender + ': Structure is invalid.');
end;

然后

procedure SomeProc1(const Struct: TMyStruct);
begin
  ValidateStruct(Struct, 'SomeProc1');
  ...
end;

...

procedure SomeProcN(const Struct: TMyStruct);
begin
  ValidateStruct(Struct, 'SomeProcN');
  ...
end;

如果我改为写类似的东西,

It would be somewhat less error-prone if I instead could write something like

procedure SomeProc1(const Struct: TMyStruct);
begin
  ValidateStruct(Struct, {$PROCNAME});
  ...
end;

...

procedure SomeProcN(const Struct: TMyStruct);
begin
  ValidateStruct(Struct, {$PROCNAME});
  ...
end;

然后,每次编译器遇到{$ PROCNAME}时,它仅将宏"替换为当前函数/过程的名称,作为字符串文字.

and then each time the compiler encounters a {$PROCNAME}, it simply replaces the "macro" with the name of the current function/procedure as a string literal.

更新

第一种方法的问题是它容易出错.例如,由于复制粘贴而容易出错,这很容易发生:

The problem with the first approach is that it is error-prone. For instance, it happens easily that you get it wrong due to copy-paste:

  procedure SomeProc3(const Struct: TMyStruct);
  begin
    ValidateStruct(Struct, 'SomeProc1');
    ...
  end;

或错别字:

procedure SomeProc3(const Struct: TMyStruct);
begin
  ValidateStruct(Struct, 'SoemProc3');
  ...
end;

或只是暂时的混乱:

procedure SomeProc3(const Struct: TMyStruct);
begin
  ValidateStruct(Struct, 'SameProc3');
  ...
end;

推荐答案

我们正在做类似的事情,只是依赖一个约定:将 const SMethodName放在函数的开头就 .
然后我们所有的例程都遵循相同的模板,然后在断言和其他引发异常的过程中使用此const.
由于const与例行程序名称非常接近,因此输入错误或出现差异的可能性很小.
YMMV当然...

We are doing something similar and only rely on a convention: putting a const SMethodName holding the function name at the very beginning.
Then all our routines follow the same template, and we use this const in Assert and other Exception raising.
Because of the proximity of the const with the routine name, there is little chance a typo or any discrepancy would stay there for long.
YMMV of course...

procedure SomeProc1(const Struct: TMyStruct);
const
  SMethodName = 'SomeProc1';
begin
  ValidateStruct(Struct, SMethodName);
  ...
end;

...

procedure SomeProcN(const Struct: TMyStruct);
const
  SMethodName = 'SomeProcN';
begin
  ValidateStruct(Struct, SMethodName);
  ...
end;

这篇关于如何在Delphi中获取当前过程/函数的名称(作为字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆