1

I would like to access the following property using RTTI

MyComponent1.Property['variable'].SubProperty

I would like something like that:

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 a subproperty of my component and I have only strings, so I cannot use Typ:=Ctx.GetType(MyComponent1.ClassInfo) and then Prop:=Typ.GetProperty('Property['variable'].Subproperty') this is not allowed. Attention in the fact that there is a paramenter for the first property. I guess I have to obtain this first property and then somehow the second property, because I cannot use this property1"."property2
Does anyone know how to do that?

Felipe
  • 253
  • 4
  • 11
  • Can you elaborate more on what you are really trying to achieve here? I suspect the final answer will be recursion :) – whosrdaddy Jun 05 '13 at 20:04
  • My goal is to set a value for the SubProperty, for example: `MyComponent1.Property['variable'].SubProperty` But in my code I have a lot of components and properties, so I have to check first MyComponent and then get the property Property['variable'] and then somehow I would like to change SubProperty, but I cannot use `subProp:= Prop.getProperty` or `Typ.getProperty('Property['variable'].subProperty')` – Felipe Jun 05 '13 at 20:20
  • You can remove the TRttiContext.Create and Ctx.Free lines – David Heffernan Jun 06 '13 at 06:32
  • I also don't really understand your question. – David Heffernan Jun 06 '13 at 06:33
  • Basically I want to access a subproperty of my component and I have only strings, so I cannot use `Typ:=Ctx.GetType(MyComponent1.ClassInfo)` and then Prop:= `Typ.GetProperty('Property['variable'].Subproperty')` this is not allowed. Attention in the fact that there is a paramenter for the first property. I guess I have to obtain this first property and then somehow the second property, because I cannot use this property1"."property2 – Felipe Jun 06 '13 at 07:27

1 Answers1

0

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.

Community
  • 1
  • 1
ThinkJet
  • 6,725
  • 24
  • 33
  • Could you give a bit of code for that? I am quite confused how to accomplished it... – Felipe Jun 05 '13 at 20:40
  • Which version of Delphi you use? RTTI support for indexed properties exists only since XE2 ... – ThinkJet Jun 05 '13 at 21:29
  • I have Delphi version 2010 – Felipe Jun 05 '13 at 21:37
  • Another thing is that I may be misunderstood your question. Did you mean that you want to access inner property value by supplying just a string which describes access path to the inner property? – ThinkJet Jun 05 '13 at 21:39
  • Yes, I have only strings and I would like to access the property, but I get problem when a property in inside another property. – Felipe Jun 05 '13 at 21:48
  • If indexed properties like in your example is not an issue for you task and you can ignore them, just use recursive function to reach desired level step by step. – ThinkJet Jun 05 '13 at 21:56