type
TMyClass = class
...
public
...
property P1: Integer Index 1 read GetInteger write SetInteger;
property P2: Integer Index 2 read GetInteger write SetInteger;
property P3: Integer Index 3 read GetInteger write SetInteger;
...
end;
是否有可能得到类属性的索引?例如,类似于
I := IndexOfProperty(TMyClass.P2);
发布于 2013-10-31 11:53:22
您可以使用RTTI来获取属性的索引。根据您的Delphi版本,您可以使用GetPropInfo
方法(仅用于已发布的属性)或通过TRttiInstanceProperty
类访问此类信息。
试试这个样本。
{$APPTYPE CONSOLE}
uses
Rtti,
SysUtils,
TypInfo;
type
TMyClass = class
private
function GetInteger(const Index: Integer): Integer;
procedure SetInteger(const Index, Value: Integer);
public
property P1: Integer Index 1 read GetInteger write SetInteger;
property P2: Integer Index 2 read GetInteger write SetInteger;
property P3: Integer Index 3 read GetInteger write SetInteger;
end;
{ TMyClass }
function TMyClass.GetInteger(const Index: Integer): Integer;
begin
end;
procedure TMyClass.SetInteger(const Index, Value: Integer);
begin
end;
var
LRttiInstanceProperty : TRttiInstanceProperty;
LRttiProperty : TRttiProperty;
Ctx: TRttiContext;
LPropInfo : PPropInfo;
begin
try
LPropInfo:= GetPropInfo(TMyClass, 'P1'); //only works for published properties.
if Assigned(LPropInfo) then
Writeln(Format('The index of the property %s is %d',[LPropInfo.Name, LPropInfo.Index]));
Ctx:= TRttiContext.Create;
try
LRttiProperty:= Ctx.GetType(TMyClass).GetProperty('P2');
if Assigned(LRttiProperty) and (LRttiProperty is TRttiInstanceProperty) then
begin
LRttiInstanceProperty := TRttiInstanceProperty(LRttiProperty);
Writeln(Format('The index of the property %s is %d',[LRttiProperty.Name, LRttiInstanceProperty.Index]));
end;
finally
Ctx.Free;
end;
except
on E:Exception do
Writeln(E.Classname, ':', E.Message);
end;
Writeln('Press Enter to exit');
Readln;
end.
发布于 2013-10-31 11:56:55
属性的RTTI包含索引。您的属性被声明为public
,因此不能通过TypInfo
单元提供的旧风格的RTTI访问它们。但是,它们可以通过Rtti
单元提供的更新样式的RTTI (D2010和更高版本)访问:
uses
Rtti;
var
Ctx: TRttiContext;
I: Integer;
begin
Ctx := TRttiContext.Create;
I := (Ctx.GetType(TMyClass).GetProperty('P2') as TRttiInstanceProperty).Index;
end;
如果您的属性被声明为published
,那么您可以使用TypInfo
RTTI:
uses
TypInfo;
var
I: Integer;
begin
I := GetPropInfo(TMyClass, 'P2').Index;
end;
https://stackoverflow.com/questions/19713584
复制相似问题