是否可以获取类属性的索引?

7
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);

@JerryDodge - 我需要公共属性的索引。 - Branko
2个回答

6
您可以使用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.

你不需要创建一个类的实例来使用GetPropInfo()。你可以传递类类型而不是对象指针。另外,TRttiInstanceProperty有它自己的Index属性,你不需要使用PropInfo.Index - Remy Lebeau
你提到了“(仅适用于已发布的属性)”,但是示例却是公共属性... - Jerry Dodge
@JerryDodge,我知道,OP必须修改类并声明属性为已发布,才能使用GetPropInfo函数。 - RRUZ
@PRUZ - 我有一个额外的问题。请参见https://dev59.com/gXjZa4cB1Zd3GeqPgaR3 - Branko

6

属性的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;

但是他可以使用$RTTI编译指示符,使公共属性也被TypeInfo覆盖,不是吗? - Arioch 'The
@Remy Lebeau - 我有一个额外的问题。请参见https://dev59.com/gXjZa4cB1Zd3GeqPgaR3 - Branko
@Arioch'The - Delphi XE4,{$RTTI EXPLICIT METHODS([vcPublic, vcPublished]) PROPERTIES([vcPublic, vcPublished])} 但是 GetPropInfo() 对于公共属性无效!? - Branko
@Branko 如果我知道就好了... 就我个人而言,我根本无法枚举类属性!http://stackoverflow.com/questions/19495258/ - Arioch 'The

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接