How To Get the Name of the Current Procedure/Function in Delphi (As a String)

后端 未结 6 1269
误落风尘
误落风尘 2020-12-29 03:47

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 com

6条回答
  •  时光说笑
    2020-12-29 04:35

    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;
    

提交回复
热议问题