2
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
實際工作?如何獲取沒有硬編碼的methodname?
我正在使用XE7,但我懷疑它在舊版本中有所不同。
'MethodName'來自'System',它似乎只適用於'published'成員。我猜你最好的選擇是使用'System.Rtti'。即使那樣,我也沒有看到使用Rtti可以完成什麼:無論如何,你正在對你的'@ TBar.Test1'進行硬編碼,不是嗎?看着堆棧可能是唯一可行的選擇,而不是Rtti。 –
@GünthertheBeautiful,這個問題是重構。如果我更改名稱,則字符串不會。那會很糟糕。看着堆棧需要debuginfo,我可能沒有。 – Johan