user3065410
user3065410

Reputation: 89

Current class method name as string

I'm wondering if there is a way with Object Pascal to get the name of the method that is currently being executed.

procedure SomeClass.SomeMethod(SomeArgs...);
begin
    { This should print 'This method name is: SomeMethod' }
    WriteLn('This method name is: ' + ???);
end;

Looks like TObject got something similar to what I need in description but I can't make it work.

 public class function TObject.MethodName(address: pointer): shortstring;

Is that the way I should go ?
If I try MethodName(self), it always returns an empty string.
How to get the pointer of the method in a class?

Upvotes: 3

Views: 2331

Answers (1)

Marco van de Voort
Marco van de Voort

Reputation: 26358

I don't know about classic Object Pascal (Apple's), but Delphi/FPC:

Before D2010: There is only RTTI if the method is published, in other cases procedure name is optimized away. Unit typinfo is your friend there, and iirc the typeinfo can be gotten by using typeinfo(@TSomeclass.Somemethod).

D2010: there are options to generate more RTTI information. See e.g. RTTI information for method pointer

Free Pascal: 2.6.x and below, as before D2010, 2.7.x as D2010, but that is still being worked on.

Upvotes: 1

Related Questions