我在 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;
如何在 TPerson 中找到电话属性,并将其作为 TObject 返回?
也许是这样的:
function FindPropByName(AObject: TObject; APropName: string): TObject;
请注意,Phone是子类,没有原始类型
感谢
答案 0 :(得分:3)
这种方式例如:
uses
TypInfo;
var
Phone: TPhone;
Person: TPerson;
begin
...
if PropIsType(Person, 'Phone', tkClass) then
Phone := GetObjectProp(Person, 'Phone') as TPhone;
...
end;