【问题标题】:Is it possible to get the index of class property?是否可以获得类属性的索引?
【发布时间】:2013-10-31 18:00:46
【问题描述】:
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 - 我需要公共财产的索引。

标签: delphi properties


【解决方案1】:

您可以使用 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
  • 您提到“(仅适用于已发布的属性)”,但示例是公共属性...
  • @JerryDodge,我知道,OP 必须修改类并将属性声明为已发布才能使用GetPropInfo 函数。
  • @PRUZ - 我还有一个问题。请看stackoverflow.com/questions/19722394/…
【解决方案2】:

属性的 RTTI 包含索引。您的属性被声明为public,因此它们无法通过TypInfo 单元提供的旧式RTTI 访问。但是,它们可以通过Rtti 单元(仅限 D2010 及更高版本)提供的更新样式的 RTTI 访问:

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 pragma 使公共属性也被 TypeInfo 覆盖,不是吗?
  • @Remy Lebeau - 我还有一个问题。请看stackoverflow.com/questions/19722394/…
  • @Arioch'The - Delphi XE4, {$RTTI EXPLICIT METHODS([vcPublic, vcPublished]) PROPERTIES([vcPublic, vcPublished])}GetPropInfo() 不适用于公共道具!?
  • @Branko 如果我只知道...就个人而言,我根本无法枚举类属性! stackoverflow.com/questions/19495258
猜你喜欢
  • 2013-05-22
  • 1970-01-01
  • 1970-01-01
  • 2018-05-06
  • 2017-01-02
  • 2014-07-07
  • 2012-07-22
  • 2021-11-22
  • 1970-01-01
相关资源
最近更新 更多