如何使用rtti访问类属性?

时间:2017-11-17 08:54:56

标签: delphi rtti

是否可以使用rtti获取类属性?下面的代码出了什么问题?

...
type
  TTest = class
  private
    class function GetCP: string; static;
  public
    class property CP: string read GetCP;
  end;

class function TTest.GetCP: string;
begin
  Result := 'ABC';
end;
...
procedure TForm1.Button5Click(Sender: TObject);
var
  oTest: TTest;
  oType: TRttiType;
begin
  oTest := TTest.Create;
  try
    oType := TRttiContext.Create.GetType(oTest.ClassType);
    ShowMessage(Length(oType.GetProperties).ToString);  // oType.GetProperties = nil !!! 
  finally
    oTest.Free;
  end;
end;  

TIA和最好的问候, 茨尔

1 个答案:

答案 0 :(得分:4)

无法通过RTTI访问类属性。

相关问题