加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

在运行时获取delphi记录中字段的偏移量

发布时间:2020-12-15 04:21:43 所属栏目:大数据 来源:网络整理
导读:给定记录类型: TItem = record UPC : string[20]; Price : Currency; Cost : Currency; ...end; 并且字段的名称为字符串,如何在记录中获取该字段的偏移量?我需要在运行时执行此操作 – 要在运行时决定要访问的字段的名称. 例: var pc : Integer; fieldNam
给定记录类型:
TItem = record
   UPC : string[20];
   Price : Currency;
   Cost : Currency;
   ...
end;

并且字段的名称为字符串,如何在记录中获取该字段的偏移量?我需要在运行时执行此操作 – 要在运行时决定要访问的字段的名称.

例:

var
   pc : Integer;
   fieldName : string;
   value : Currency;
begin
   pc := Integer(@item);                    // item is defined and filled elsewhere
   fieldName := Text1.Text;                 // user might type 'Cost' or 'Price' etc
   Inc(pc,GetItemFieldOffset(fieldName));  // how do I implement GetItemFieldOffset?
   value := PCurrency(pc)^;
   ..

我正在使用Delphi 7.

解决方法

你不能. Delphi 7不会为记录发出RTTI.还有其他选项(如前面的答案所示),但那些需要手动映射“字段名称” – > “偏移”.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读