Delphi:解析未知结构的记录 [英] Delphi: Parsing a record of unknown structure

查看:84
本文介绍了Delphi:解析未知结构的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以以编程方式遍历所有字段而无需显式地执行 Record.somefield?

Is there a way to take a record and go through all its fields programatically without explicitly doing "Record.somefield"?

我想做的是一个概括用于将记录保存/加载为INI文件的函数,这样我就可以在记录结构中添加或删除字段,而不必每次删除或向记录添加字段时都重写存储/加载函数。

What I want to do is a generalized function to save/load a record as an INI file so that I can add or remove fields from my record structure without having to rewrite the save/load functions each time I remove or add fields to the record.

换句话说,我想以这种方式(伪代码)进行迭代:

In other words I want to iterate in some way like this (pseudo code):

THuman = record  
  age: integer;  
  country: string ;  
end;  
...  
myPerson: THuman;  
...  
    foreach Field in myPerson do
    begin
      if Field.value is String then ini.WriteString( 'Group', Field.Name, Field.value );
      if Field.value is integer then ini.WriteInteger( 'Group', Field.Name, Field.value );
    end;

顺便说一句,我要这样做的原因是因为使用无界字符串时无法保存键入的文件。这些文件对于人类来说也是有益的。所以我要保存它们和INI文件。

BTW, the reason I want to do this is because I can't save a typed filed when using non-bounded strings. Also it is beneficial to me that the files are human readable. So I am saving them and INI files.

推荐答案

没有机会。记录没有附加的运行时类型信息。如果您改用具有已发布属性的类,则有可能,请参见jvcl中的TApplicationStorage / TFormStorage组件,Delphi DFM流系统和各种Internet站点。

No chance at that. A record has no runtime type information attached to it. If you used a class with published properties instead, it would be possible, see the TApplicationStorage/TFormStorage components in the jvcl, the Delphi DFM streaming system and various internet sites for examples.

这篇关于Delphi:解析未知结构的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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