如何获取已声明的TList中元素的类型 [英] How to get the type of the elements in a declared TList

查看:206
本文介绍了如何获取已声明的TList中元素的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法来获取已声明但未实例化的TList的元素类型。我可以像这样捕获对象属性的类:

I'd like to know if there is a way to get the type of the elements of a declared, but not instantiated, TList. I can capture the class of an object property like this:

MyList: TList<TMyObject> read FMyList;

MyRTTIProperty: TRttiProperty;    
NewObject: TObject;
PropertyClass: TClass;

MyRttiProperty := MyRttiType.GetProperty('MyList');
PropertyClass := MyRTTIProperty.PropertyType.Handle.TypeData.ClassType; // (returns TList<TMyObject>)
NewObject := PropertyClass.Create;

这样我可以正确实例化列表。但是现在我也想获取元素的类TMyObject,以便我可以实例化它们,填充其属性并添加到列表中。我该怎么办?

This way I can instantiate my list correctly. But now I also want to get the class of the elements, TMyObject, so that I can instantiate them, fill its properties and add to the list. How can I do that?

预先感谢。

推荐答案

据我所知,RTTI不够灵活,无法为您提供有关通用参数的信息。您可能必须手动解析 MyRTTIProperty.PropertyType.Name TList< TMyObject> )的值以提取通用类型名称( TMyObject ),然后使用 TRttiContext.FindType()获取该类型的RTTI,然后最后,您可以根据需要创建其 ClassType 的实例。

As far as I know, RTTI is not flexible enough to give you information about Generic parameters. You will likely have to manually parse the value of MyRTTIProperty.PropertyType.Name (TList<TMyObject>) to extract the Generic type name (TMyObject), then use TRttiContext.FindType() to get the RTTI for that type, and then finally you can create instances of its ClassType as needed.

这篇关于如何获取已声明的TList中元素的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆