Get a sub property of a component in Delphi using RTTI

后端 未结 1 875
萌比男神i
萌比男神i 2021-01-25 10:13

I would like to access the following property using RTTI

MyComponent1.Property[\'variable\'].SubProperty

I would like something like that:

1条回答
  •  面向向阳花
    2021-01-25 10:57

    Index properties as all other properties (except direct references to object fields) just a shortcut to getXXX and setXXX methods.

    Try that way:

    1. Get all indexed properties of Ctx.GetType(MyComponent1.ClassInfo) with GetDeclaredIndexedProperties or GetIndexedProperties

    2. Search desired Property in returned array of TRttiIndexedProperty instances.

    3. Get write method description object from WriteMethod property of TRttiIndexedProperty object found.

    4. Get method parameters description if you need it with GetParameters call.

    5. Call Invoke method of method description object with constructed parameter(s) list to set a property value.

    Update

    This works only in Delphi versions from XE2 and above.

    In previous versions indexed properties can be adopted for RTTI only using things like discussed in this question.

    0 讨论(0)
提交回复
热议问题