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

后端 未结 6 1315
误落风尘
误落风尘 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:39

    I've solved similar problems through design. Your example confuses me because you seem to already be doing this.

    You wrap your validation functions once like this:

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

    Then instead of repeatedly calling:

    ValidateStruct(Struct, 'SomeProc3");
    

    You call:

    SomeValidateProc3(Struct);
    

    If you have a typo, the compiler will catch it:

    SoemValidateProc3(Struct);
    

    If you use a more meaningful name for your wrapper functions like "ValidateName", the code becomes more readable also.

提交回复
热议问题