The only way to recursively access child TCollection
objects, without knowing the class types of the owning TCollectionItem
objects so you can type-cast them, is to use the VCL's RTTI information.
In C++Builder versions prior to XE, VCL-based RTTI is only available for __published
properties. Given a TCollectionItem
(or any general TObject
) object pointer, you can use the GetPropList()
function declared in TypInfo.hpp
to retreive a list of that object's published property information. You can then loop through that list, checking for any properties that report a TypeKind value of tkClass
. When you find one, use the GetObjectProp()
function to retreive that property's TObject
pointer value, and then use dynamic_cast
to make sure it is really a TCollection
object before you access its child TCollectionItem
objects.
In C++Builder 2010, a new Enhanced RTTI system was introduced, declared in Rtti.hpp
, that provides information for all members of a class, including non-published properties and fields. With this enhanded RTTI, a child TCollection
does not need to be declared as a __published
property anymore. Under this system, you would use the TRttiContext
class to access a TRttiType
object for your recursion's starting TCollectionItem
object, then use the TRttiType::GetFields()
and TRttiType::GetProperties()
methods to look for child TRttiField
and TRttiProperty
items that report a TypeKind of tkClass
, then use the TRttiField::GetValue()
and TRttiProperty::GetValue()
methods to get the TObject
object pointer that can be type-casted to a TCollection
pointer with dynamic_cast
.