在运行时获取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。
Given a record type:
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.
Example:
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)^;
..
I'm using Delphi 7.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你不能。 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".
正如 alex 所说,Delphi 7 不会发出记录的 RTTI,因此您无法在运行时检索所需的信息。但是,在更高版本(Delphi 2010+)中,它会执行此操作,并且以下代码:
将生成 'TItem {{name=UPC,offset=0}{name=Price,offset=24}{name=Cost,offset=32} 您还可以使用以下
方法在特定实例中设置字段值(尽管您实际上还应该验证类型):
As alex said, Delphi 7 doesn't emit RTTI for records, so you can't retrieve the required info at runtime. However, in later versions (Delphi 2010+) it does, and the following code:
will produce 'TItem {{name=UPC,offset=0}{name=Price,offset=24}{name=Cost,offset=32}}'
You can also set the field value in a particular instance (although you should really also verify the type) using:
以下内容适用于您的简化场景,但我怀疑是否有可能为这种事情创建一个通用函数。
我能想到的最好的办法是添加某种注册对象,但它仍然需要您注册您需要偏移量的所有记录。
Following would work for your simplified scenario but I doubt it will be possible to make a generic function for this kind of thing.
The best I can think if is to add some kind of registration object but it still would require you to register all the records you need an offset from.
这是您要找的吗
Is this what you are looking for