getTrprop在对象上找不到属性
我试图获取具有其名称的图形的属性:
var key := GetStrProp(table.Item, keyName);
var value := GetStrProp(table.Item, valueName);
上面的代码返回错误,说它无法在对象上找到属性'代码'(Keyname包含“代码”)。 ValueName也是如此。
我使用以下代码测试了对象的内容,它确实包含属性代码,并且该值还包含我期望的。
var item := table.Item as TdciPROJECTRESULTAATTYPE;
var code := item.CODE;
var value := item.DESCRIPTION;
前2行有什么问题?
I'm trying to get the properties of a TObject with their name like so:
var key := GetStrProp(table.Item, keyName);
var value := GetStrProp(table.Item, valueName);
The code above returns an error saying it cannot find property 'CODE' on the object (the keyName contains 'CODE'). The same happens for the valueName.
I tested the content of the object with the code below, it does contain the property CODE and the value also contains what I expect.
var item := table.Item as TdciPROJECTRESULTAATTYPE;
var code := item.CODE;
var value := item.DESCRIPTION;
What is wrong with the first 2 lines?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码没有错,但是TDCiprojectResultaAttype类可能有些“错误”。
gettrprop
是来自typinfo
单元的Delphi的“旧式” RTTI方法之一。旧的RTTI仅生成类型信息,以使用{M+}编译器指令(及其后代)编译的对象的已发布属性。 tdciprojectResultaAttype很可能没有那些旧式的RTTI。如果您拥有现代版本的Delphi,它提供了更多的“现代” RTTI函数(在单位
rtti
中),可以访问有关对象的更多信息(尽管我目前不记得确切的纳入规则)。这些 May 允许您通过RTTI访问您的属性。There's nothing wrong with your code, but there might be something "wrong" with the TdciPROJECTRESULTAATTYPE class.
GetStrProp
is one of Delphi's "old-style" RTTI methods from theTypInfo
unit. The old RTTI was only generating type information for published properties of objects compiled with the {M+} compiler directive (and their descendants). TdciPROJECTRESULTAATTYPE most likely does not have those old-style RTTI.If you have a modern version of Delphi, it offers more "modern" RTTI functions(in unit
RTTI
), that can access way more informations about objects (Though, I don't remember at this time the exact inclusion rules). Those may allow you to access your property through RTTI.感谢肯指向我指向正确的方向。我创建了一个小功能,可以很好地完成我的需求。这是代码。
您可以这样使用:
Thanks to Ken for pointing me in the right direction. I've created a little function that does what I need just fine. Here's the code.
You can use it like so: