How to perform a cast of a TRTTIProperty for an object

Viewed 484

I'm studying RTTI in Delphi-Tokyo to create an ORM, but I'm having trouble accessing properties that are also objects. In the code below how can I perform the Cast of the Prop for id?

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, rtti, Vcl.StdCtrls;

type
  TIntField = class(TObject)
  private
    fDisplayNme: string;
  public
    constructor Create(DisplayName: string);
  published
    property DisplayName: string read fDisplayNme write fDisplayNme;
  end;

  TSale = class(TObject)
  private
    fIdSale: TIntField;
  public
    constructor Create;
  published
    property IdSale: TIntField read fIdSale write fIdSale;
  end;

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


{ TIntField }

constructor TIntField.Create(DisplayName: string);
begin
  fDisplayNme:= DisplayName;
end;

{ TSale }

constructor TSale.Create;
begin
  fIdSale:= TIntField.Create('idSale');
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Context: TRttiContext;
  TypObj:  TRttiType;
  Prop:    TRttiProperty;
  sale:    TSale;
  id:      TIntField;
begin
  sale:= TSale.Create;

  Context:= TRttiContext.Create;

  TypObj:= Context.GetType(sale.ClassInfo);

  prop:= TypObj.GetProperty('IdSale');

  id:= Prop as TIntField;  //Would you like to do this or something equivalent

  ShowMessage(id.DisplayName);
end;

end.
1 Answers
Related