使用RTTI获取Delphi中的组件的子属性 [英] Get a sub property of a component in Delphi using RTTI
问题描述
我想使用RTTI访问以下属性
I would like to access the following property using RTTI
MyComponent1.Property['variable'].SubProperty
我想要这样:
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;
基本上我想访问我的组件的子属性,我只有字符串,所以我不能使用 Typ:= Ctx.GetType(MyComponent1.ClassInfo)
然后 Prop:= Typ.GetProperty('Property ['variable']。Subproperty')
这是不允许的。注意事实上,第一个财产有一个参考。我想我必须获得这个第一个属性,然后不知何故第二个属性,因为我不能使用此属性1。property2
有没有人知道如何做?
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?
推荐答案
索引属性作为所有其他属性(除了直接引用对象字段)只是快捷方式 getXXX
和 setXXX
方法。
Index properties as all other properties (except direct references to object fields) just a shortcut to getXXX
and setXXX
methods.
尝试这样:
-
使用 Ctx.GetType(MyComponent1.ClassInfo)的所有索引属性/Libraries/XE2/en/System.Rtti.TRttiType.GetDeclaredIndexedPropertiesrel =nofollow>
GetDeclaredIndexedProperties
或
Get all indexed properties of
Ctx.GetType(MyComponent1.ClassInfo)
withGetDeclaredIndexedProperties
orGetIndexedProperties
在属性 en /系统。 Rtti.TRttiIndexedProperty_Propertiesrel =nofollow> TRttiIndexedProperty
实例。
Search desired Property
in returned array of TRttiIndexedProperty
instances.
获取写方法描述对象来自 WriteMethod
属性 TRttiIndexedProperty
找到对象
Get write method description object from WriteMethod
property of TRttiIndexedProperty
object found.
获取方法参数说明如果你需要使用 GetParameters
电话。
Get method parameters description if you need it with GetParameters
call.
致电 调用
方法描述对象的方法,并使用构造的参数列表设置属性值。 p>
更新
只有在XE2及以上的Delphi版本中。
This works only in Delphi versions from XE2 and above.
在以前的版本中,索引属性可以用于RTTI,只能使用这个问题。
In previous versions indexed properties can be adopted for RTTI only using things like discussed in this question.
这篇关于使用RTTI获取Delphi中的组件的子属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!