Search code examples
delphidelphi-7rtti

Find property as TObject in Delphi 7


I have two type of classes like this in Delphi 7:

  TPhone = Class(TPersistent)
  private
    FNumber: string;
  published
    property Number: string read FNumber write FNumber;
  end;

  TPerson = class(TPersistent)
  private
    FFirstName: string;
    FPhone: TPhone;
  public
    constructor Create;
  published
    property FirstName: string40 read FFirstName write FFirstName;
    property Phone: TPhone read FPhone write FPhone;
  end;

How can I find Phone property in TPerson by it's name and return it as a TObject ?

Maybe something like this:

  function FindPropByName(AObject: TObject; APropName: string): TObject;

Note that Phone is subclass and no primitive type

thanks


Solution

  • This way for instance:

    uses
      TypInfo;
    
    var
      Phone: TPhone;
      Person: TPerson;
    begin
      ...
      if PropIsType(Person, 'Phone', tkClass) then
        Phone := GetObjectProp(Person, 'Phone') as TPhone;
      ...
    end;