2015-05-12 96 views
1

我有兩個類型的這樣的班德爾福7查找財產TObject的德爾福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; 

我如何在找到電話財產TPerson由它的名字,並返回它作爲a TObject

也許是這樣的:

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

注意,電話是子類,沒有原始類型

感謝

回答

3

這種方式,例如:

uses 
    TypInfo; 

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