RTTI:如何获取声明为类属性的动态数组的值

发布于 2024-10-01 10:33:52 字数 1046 浏览 5 评论 0原文

请帮我。

我寻找存在的问题,但没有找到如何在delphi类的动态数组中获取项目(声明为类)的所有已发布属性(我使用Delphi 7 IDE(我不能使用其他版本))

我有这个代码:

  TObjectList = array of TObject;
  TSubClass = class(TObject)
  private
    FFirstName: string;
    FLastName: string;
    FDOB: TDateTime;
    FArray : TObjectList;
  published
    property FirstName: string read FFirstName write FFirstName;
    property LastName: string read FLastName write FLastName;
    property DOB: TDateTime read FDOB write FDOB;
    property MyArray : TObjectList read FArray write FArray ;
  end;

  TListSubClass = array of TSubClass;

  TPersonList = class(TObject)
  private
    FSubClasses: TListSubClass;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property SubClasses: TListSubClass read FSubClasses write FSubClasses;
  end;

我链接到 TPersonList 类的 Elem (MyVariable : TPersonList)。

我如何使用 RTTI 获取我的 FSubClasses 和 FArray 数组项的所有已发布属性数据?

我如何使用 RTTI 将新数据设置到 FSubClasses?

谢谢你, 谢尔盖.

Please, help me.

I looked for exists questions and no found how i can get all published property of items (declared as Class) in dynamic array in delphi class (i use Delphi 7 IDE (i can't use other version))

I have this code:

  TObjectList = array of TObject;
  TSubClass = class(TObject)
  private
    FFirstName: string;
    FLastName: string;
    FDOB: TDateTime;
    FArray : TObjectList;
  published
    property FirstName: string read FFirstName write FFirstName;
    property LastName: string read FLastName write FLastName;
    property DOB: TDateTime read FDOB write FDOB;
    property MyArray : TObjectList read FArray write FArray ;
  end;

  TListSubClass = array of TSubClass;

  TPersonList = class(TObject)
  private
    FSubClasses: TListSubClass;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property SubClasses: TListSubClass read FSubClasses write FSubClasses;
  end;

I have link to Elem of TPersonList class (MyVariable : TPersonList).

How i can using RTTI get all published property's data of my FSubClasses and FArray array items?

How i can set new data to FSubClasses using RTTI?

Thank you,
Sergey.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绳情 2024-10-08 10:33:52

你所谓的“动态数组”并不是Delphi世界中所谓的“动态数组”。
“动态数组”被定义为 MyVar:例如整数数组。在您的类中,您只有 TList 后代。这些 TList 后代是某种动态存储,但它称为 TList(或 TObjectList),而不是“动态数组”。

所以只需使用 TypInfo 单元即可。

  • GetPropList 将为您提供所有属性的列表。
  • 然后为映射类的每个 PPropInfo 项调用 GetObjectProp,并检索每个属性的实例。
  • 调用 GetStrProp 检索字符串发布属性的内容;
  • 调用 GetOrdProp 检索整数已发布属性的内容。
  • 调用 GetFloatProp 获取浮点值,如 TDateTime。

对于类发布的属性,在调用 GetObjectProp 后,检查返回的实例类型,并根据其类(TObjectList 或 TListSubClass)枚举其内容。

我们在开源 ORM 中使用的正是这种方法(我们专门使用了一些面向对象的类来进行属性访问,因此我们不需要typinfo 单元)。请参阅http://synopse.info/fossil/finfo?name=SQLite3/SQLite3Commons。帕斯

What you call "dynamic array" is not what is called a "dynamic array" in the Delphi world.
A "dynamic array" is defined as MyVar: array of integer for instance. In your classes, you've only TList descendants. These TList descendants are some kind of dynamic storage, but it's called a TList (or TObjectList), instead of "dynamic array".

So just use the TypInfo unit.

  • GetPropList will get you the list of all properties.
  • Then call GetObjectProp for every PPropInfo item which maps a class, and retrieve the instance of every property.
  • Call GetStrProp to retrieve the content of a string published property;
  • Call GetOrdProp to retrieve the content of an integer published property.
  • Call GetFloatProp to get a floating point value, like a TDateTime.

In case of a class published property, after having called GetObjectProp, check the returned instance type, and enumerate its content, according to its class (TObjectList or TListSubClass).

It's such a method we use in our Open Source ORM (we've dedicated some object-oriented classes for properties access, so we don't need the typinfo unit). See http://synopse.info/fossil/finfo?name=SQLite3/SQLite3Commons.pas

A君 2024-10-08 10:33:52

查看 TypInfo 单元的 GetDynArrayPropGetPropList

GetDynArrayProp 返回指向基础数组的指针,然后您可以将其转换为正确的数组类型。

GetPropList 返回一个指向您传入的类的所有属性的属性信息数组的指针。

GetPropList 返回的 TPropInfo 记录具有与属性关联的 getter 和 setter 方法的地址信息,您可以使用它们分别调用 getter 或 setter。

一般来说,您应该更深入地了解 Delphi 帮助或在线文档中的 TypInfo 单元:

http://docwiki.embarcadero.com/VCL/en/TypInfo

Have a look at GetDynArrayProp and GetPropList of unit TypInfo.

GetDynArrayProp returns a pointer to the underlying array, you can then cast it to the correct array type.

GetPropList returns a pointer to an array of property information to all properties of the class you pass in.

The TPropInfo record that you get back from GetPropList has information on the address of the getter and setter methods associated with a property, you can use them to call the getter or setter respectively.

In general you should have a deeper look on the TypInfo unit in your Delphi help or at the online documentation:

http://docwiki.embarcadero.com/VCL/en/TypInfo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文