在运行时获取delphi记录中字段的偏移量 [英] Get the offset of a field in a delphi record at runtime

查看:156
本文介绍了在运行时获取delphi记录中字段的偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出记录类型:

TItem = record
   UPC : string[20];
   Price : Currency;
   Cost : Currency;
   ...
end; 

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

And the name of a field as a string, how can I get the offset of that field within the record? I need to do this at runtime - the name of the field to access is decided at runtime.

示例:

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)^;
   ..

我正在使用Delphi7.

I'm using Delphi 7.

推荐答案

您不能. Delphi 7不会发出RTTI记录.还有其他选项(如先前的答案所示),但是需要手动映射字段名称"->偏移".

You can't. Delphi 7 does not emit RTTI for records. There are other options (as seen the previous answers) but those require manual mapping of "Field Name" -> "Offset".

这篇关于在运行时获取delphi记录中字段的偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆