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.如果我运行这个程序,什么都不会显示。
我如何让MethodName真正发挥作用?
我正在使用XE7,但我怀疑它在旧版本中是不同的。
发布于 2015-05-19 19:03:33
MethodName要求发布方法。满足这样的要求:
type
TBar = class
published
procedure Test1; virtual;
end;如果要为未发布的成员获取方法名称,请使用新的样式RTTI。就像这样:
{$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 Method.CodeAddress=@TBar.Test1 then
Writeln(Method.Name);
end.当然,您可以将其打包到一个函数中,该函数将返回给定类型和代码地址的方法名。
https://stackoverflow.com/questions/30333483
复制相似问题