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

给定记录类型:
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.还有其他选项(如前面的答案所示),但那些需要手动映射“字段名称” – > “偏移”.

相关文章

 从网上看到《Delphi API HOOK完全说明》这篇文章,基本上都...
  从网上看到《Delphi API HOOK完全说明》这篇文章,基本上...
ffmpeg 是一套强大的开源的多媒体库 一般都是用 c/c+&#x...
32位CPU所含有的寄存器有:4个数据寄存器(EAX、EBX、ECX和ED...
1 mov dst, src dst是目的操作数,src是源操作数,指令实现的...