在 public 属性 上使用 GetPropInfo
using GetPropInfo on public property
据我所知,自从 Delphi 2010 年以来,我不仅可以在已发布的版本上使用 RTTI,而且还可以在 public 属性 上使用。我有一个旧的 Delphi 7 代码,它也在 XE7 下工作,但我仍然无法访问 public 属性。
代码如下:
uses
System.TypInfo;
procedure TForm1.GetPublicProp;
var
AColumn: TcxGridDBColumn;
APropInfo: PPropInfo;
begin
AColumn := MycxGridDBTableView.Columns[0];
APropInfo := GetPropInfo(AColumn, 'Index');
if (APropInfo = nil) then
showmessage('not found');
end;
(TcxGridDBColumn 是 TcxGrid > DevExpress 组件中的一列)
显然我错过了什么或者我完全误解了 RTTI 在 XE 下的工作方式并且仍然无法访问 public 属性?
一个片段,它使用新的 TRTTIContext 记录作为入口点来获取类型及其属性。
请注意,它并不明确需要 TypInfo 单元。您使用原始 PTypeInfo 获得 RTTIType,但您可以只传递 AnyObject.ClassType,它将被视为 PTypeInfo。
从类型中,您可以获得一组属性,我相信您必须迭代才能找到正确的属性。
uses
System.Rtti;
type
TColumn = class
private
FIndex: Integer;
public
property Index: Integer read FIndex write FIndex;
end;
var
AnyObject: TObject;
Context: TRttiContext;
RType: TRttiType;
Prop: TRttiProperty;
begin
AnyObject := TColumn.Create;
TColumn(AnyObject).Index := 10;
try
// Initialize the record. Doc says it's needed, works without, though.
Context := TRttiContext.Create;
// Get the type of any object
RType := Context.GetType(AnyObject.ClassType);
// Iterate its properties, including the public ones.
for Prop in RType.GetProperties do
if Prop.Name = 'Index' then
begin
// Getting the value.
// Note, I could have written AsInteger.ToString instead of StrToInt.
// Just AsString would compile too, but throw an error on int properties.
ShowMessage(IntToStr(Prop.GetValue(AnyObject).AsInteger));
// Setting the value.
Prop.SetValue(AnyObject, 30);
end;
finally
AnyObject.Free;
end;
end;
据我所知,自从 Delphi 2010 年以来,我不仅可以在已发布的版本上使用 RTTI,而且还可以在 public 属性 上使用。我有一个旧的 Delphi 7 代码,它也在 XE7 下工作,但我仍然无法访问 public 属性。
代码如下:
uses
System.TypInfo;
procedure TForm1.GetPublicProp;
var
AColumn: TcxGridDBColumn;
APropInfo: PPropInfo;
begin
AColumn := MycxGridDBTableView.Columns[0];
APropInfo := GetPropInfo(AColumn, 'Index');
if (APropInfo = nil) then
showmessage('not found');
end;
(TcxGridDBColumn 是 TcxGrid > DevExpress 组件中的一列)
显然我错过了什么或者我完全误解了 RTTI 在 XE 下的工作方式并且仍然无法访问 public 属性?
一个片段,它使用新的 TRTTIContext 记录作为入口点来获取类型及其属性。
请注意,它并不明确需要 TypInfo 单元。您使用原始 PTypeInfo 获得 RTTIType,但您可以只传递 AnyObject.ClassType,它将被视为 PTypeInfo。
从类型中,您可以获得一组属性,我相信您必须迭代才能找到正确的属性。
uses
System.Rtti;
type
TColumn = class
private
FIndex: Integer;
public
property Index: Integer read FIndex write FIndex;
end;
var
AnyObject: TObject;
Context: TRttiContext;
RType: TRttiType;
Prop: TRttiProperty;
begin
AnyObject := TColumn.Create;
TColumn(AnyObject).Index := 10;
try
// Initialize the record. Doc says it's needed, works without, though.
Context := TRttiContext.Create;
// Get the type of any object
RType := Context.GetType(AnyObject.ClassType);
// Iterate its properties, including the public ones.
for Prop in RType.GetProperties do
if Prop.Name = 'Index' then
begin
// Getting the value.
// Note, I could have written AsInteger.ToString instead of StrToInt.
// Just AsString would compile too, but throw an error on int properties.
ShowMessage(IntToStr(Prop.GetValue(AnyObject).AsInteger));
// Setting the value.
Prop.SetValue(AnyObject, 30);
end;
finally
AnyObject.Free;
end;
end;