我有一个这样的 TForm:
TMyForm = class (TForm)
[MyAttr('Data')]
edit1: TEdit;
private
FData: String;
end
当我尝试通过 RTTI 获取此表单的字段时,我只得到 edit1 字段,而不是 FDATA,当我查询 edit1 字段属性时,得到一个空数组。
对于另一个不继承 TForm 的类,一切正常。为什么?
编辑样本
type
{$RTTI EXPLICIT FIELDS([vcPrivate,vcProtected, vcPublic])}
TForm3 = class(TForm)
[TCustomAttribute]
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
[TCustomAttribute]
FData: String;
public
{ Public declarations }
end;
var
Form3: TForm3;
implementation
{$R *.dfm}
procedure TForm3.Button1Click(Sender: TObject);
var
LCtx: TRttiContext;
LField: TRttiField;
LAttr: TCustomAttribute;
begin
for LField in LCtx.GetType(Self.ClassInfo).GetDeclaredFields do
begin
Memo1.Lines.Add(lField.Name);
for LAttr in LField.GetAttributes do
Memo1.Lines.Add(#9+LAttr.ClassName);
end;
end;
end.
单击 button1 的结果:
FData
TCustomAttribute
Button1
Memo1