delphi - 在 Delphi 7 中查找 TObject 属性

标签 delphi delphi-7 rtti

我在 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 中的 Phone 属性并将其作为 TObject 返回?

也许是这样的:

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

请注意,Phone 是子类,不是原始类型

谢谢

最佳答案

例如这样:

uses
  TypInfo;

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

关于delphi - 在 Delphi 7 中查找 TObject 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30189196/

相关文章:

c++ - `type_info::before` 有什么用?

delphi - 在 Firemonkey 中打印时客户端的格式设置发生变化

delphi - 格式和指针/十六进制值(内存覆盖)

delphi - 不兼容的类型 : 'PPointerList' and 'TPointerList' in Delphi XE4

sql-server - 如何在 SQL Server 中将列的数据类型从整数更改为文本?

delphi - 德尔福·安西丝特斯(Delphi Ansistrings)

c++ - constexpr 和 RTTI

Delphi Win32 TXMLDocument 不能从线程中实例化和使用?

delphi - 为什么 Indy 10 会报告内存泄漏?

c++ - decltype 导致 is_same<T, T> 失败(相同类型)