getTrprop在对象上找不到属性

发布于 2025-01-28 03:13:27 字数 464 浏览 6 评论 0原文

我试图获取具有其名称的图形的属性:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独闯女儿国 2025-02-04 03:13:27

您的代码没有错,但是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 the TypInfo 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.

命比纸薄 2025-02-04 03:13:27

感谢肯指向我指向正确的方向。我创建了一个小功能,可以很好地完成我的需求。这是代码。

class function TComboboxHelper<TTable>.GetPropertyValue(obj: TObject; name: string) : TValue;
begin
  Result              := nil;

  var rttiContext     := TRttiContext.Create;

  try
    var classType     := obj.ClassType;
    var itemType      := rttiContext.GetType(classType);
    var propType      := itemType.GetProperty(name);

    Result            := propType.GetValue(obj);

  finally
    rttiContext.Free;

  end;

end;

您可以这样使用:

    var key           := GetPropertyValue(table.Item, keyName).AsString;
    var value         := GetPropertyValue(table.Item, valueName).AsString;

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.

class function TComboboxHelper<TTable>.GetPropertyValue(obj: TObject; name: string) : TValue;
begin
  Result              := nil;

  var rttiContext     := TRttiContext.Create;

  try
    var classType     := obj.ClassType;
    var itemType      := rttiContext.GetType(classType);
    var propType      := itemType.GetProperty(name);

    Result            := propType.GetValue(obj);

  finally
    rttiContext.Free;

  end;

end;

You can use it like so:

    var key           := GetPropertyValue(table.Item, keyName).AsString;
    var value         := GetPropertyValue(table.Item, valueName).AsString;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文