Reputation: 76547
program Project37;
{$APPTYPE CONSOLE}
{$RTTI EXPLICIT METHODS([vcPrivate,vcProtected,vcPublic, vcPublished])}
type
TBar = class
procedure Test1; virtual;
end;
TFoo = class(TBar)
end;
procedure TBar.Test1;
begin
WriteLn(MethodName(@TBar.Test1)); //compiles, but does not show anything
//WriteLn(MethodName(@Self.Test1)); //does not compile
end;
var
Foo: TBar;
begin
Foo:= TFoo.Create;
Foo.Test1;
Foo.Free
Foo:= TBar.Create;
Foo.Test1;
Foo.Free;
ReadLn;
end.
If I run the program nothing shows.
How do I get MethodName
to actually work?
I'm using XE7, but I doubt it's different in older versions.
Upvotes: 2
Views: 1505
Reputation: 612894
MethodName
requires the method to be published. Meet that requirements like so:
type
TBar = class
published
procedure Test1; virtual;
end;
If you want to get method names for members that are not published, use new style RTTI. Like so:
{$APPTYPE CONSOLE}
{$RTTI EXPLICIT METHODS([vcPrivate,vcProtected,vcPublic, vcPublished])}
uses
System.Rtti;
type
TBar = class
private
procedure Test1;
end;
procedure TBar.Test1;
begin
end;
var
ctx: TRttiContext;
Method: TRttiMethod;
begin
for Method in ctx.GetType(TBar).GetMethods do
if [email protected] then
Writeln(Method.Name);
end.
Naturally you could wrap this up into a function that would return a method name given a type and a code address.
Upvotes: 6