为什么在 VCL 控件上调用 TRttiContext.GetType 时,某些属性会重复(例如Action
和Align
)而其他属性不会( )?AlignWithMargins
uses
System.RTTI,
System.Generics.Collections,
System.Generics.Defaults;
//....
procedure TForm11.btnShowPropertiesClick(Sender: TObject);
var
R: TRttiContext;
Props: TArray<TRttiProperty>;
Prop : TRttiProperty;
begin
memo1.Clear;
R := TRttiContext.Create;
Props := R.GetType(Sender.ClassType).GetProperties;
//Sort properties by name
TArray.Sort<TRttiProperty>(props,
TComparer<TRttiProperty>.Construct(
function(const Left, Right: TRttiProperty): Integer
begin
result := CompareText(Left.Name, Right.Name);
end
)
);
for prop in Props do
begin
try
Memo1.Lines.Add(
Prop.Name + ' : ' +
Prop.PropertyType.ToString + ' = ' +
Prop.GetValue(Sender).ToString);
except
Memo1.Lines.Add(Prop.Name + ' generated an exception');
end;
end;
end;
输出
动作:TBasicAction =(空) 动作:TBasicAction =(空) 对齐:TAlign = alNone 对齐:TAlign = alNone AlignDisabled : Boolean = False AlignWithMargins : Boolean = False 锚点:TAnchors = [akLeft,akTop] 锚点:TAnchors = [akLeft,akTop] BiDiMode:TBiDiMode = bdLeftToRight BiDiMode:TBiDiMode = bdLeftToRight ...