Get sub component property in Delphi using RTTI

I would like to access the next property using RTTI

MyComponent1.Property['variable'].SubProperty 

I would like something like this:

 var Ctx: TRttiContext; Typ: TRttiType; SubTyp: TRttiType; Prop: TRttiProperty; SubProp: TRttiProperty; begin Ctx:= TRttiContext.Create; Typ:= Ctx.GetType(MyComponent1.ClassInfo); Prop:= Typ.GetProperty('Property['variable'].Subproperty') //not possible Prop.SetValue(MyComponent1.Property['variable'],'500'); end; 

Basically I want to access the subtask of my component, and I only have strings, so I cannot use Typ:=Ctx.GetType(MyComponent1.ClassInfo) and then Prop:=Typ.GetProperty('Property['variable'].Subproperty') this is not valid. Attention is that for the first property there is parasection. I assume that I need to get this first property, and then somehow the second property, because I can not use this property1 "." Property2
Does anyone know how to do this?

+1
source share
1 answer

Index properties, like all other properties (except for direct references to object fields), are simply combined with the getXXX and setXXX .

Try the following:

  • Get all indexed properties of Ctx.GetType(MyComponent1.ClassInfo) using GetDeclaredIndexedProperties or GetIndexedProperties

  • Find the desired Property in the returned array of TRttiIndexedProperty instances.

  • Get the object of the description of the recording method from the WriteMethod property of the found TRttiIndexedProperty object.

  • Get a description of the method parameters if you need it using GetParameters .

  • Invoke method to describe a method object with the constructed parameter (s) to set the property value.

Update

This only works on Delphi versions from XE2 and higher.

In previous versions, indexed properties can only be accepted for RTTI using things such as those discussed in this question .

0
source

All Articles