delphirtti

How to access a class property with rtti?


Is it possible to get a class property with a rtti? What's wrong with the code below?

...
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 and best regards, Branko


Solution

  • Class properties cannot be accessed via RTTI.