RTTI:如何获取声明为类属性的动态数组的值
请帮我。
我寻找存在的问题,但没有找到如何在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你所谓的“动态数组”并不是Delphi世界中所谓的“动态数组”。
“动态数组”被定义为 MyVar:例如整数数组。在您的类中,您只有 TList 后代。这些 TList 后代是某种动态存储,但它称为 TList(或 TObjectList),而不是“动态数组”。
所以只需使用 TypInfo 单元即可。
对于类发布的属性,在调用 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.
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
查看
TypInfo
单元的GetDynArrayProp
和GetPropList
。GetDynArrayProp
返回指向基础数组的指针,然后您可以将其转换为正确的数组类型。GetPropList
返回一个指向您传入的类的所有属性的属性信息数组的指针。从
GetPropList
返回的TPropInfo
记录具有与属性关联的 getter 和 setter 方法的地址信息,您可以使用它们分别调用 getter 或 setter。一般来说,您应该更深入地了解 Delphi 帮助或在线文档中的
TypInfo
单元:http://docwiki.embarcadero.com/VCL/en/TypInfo
Have a look at
GetDynArrayProp
andGetPropList
of unitTypInfo
.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 fromGetPropList
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