Delphi如何遍历packed record字段无需逐个指定字段名
Delphi Packed Record字段遍历与序列化版本控制实现
开发环境
- 适配Delphi版本:Sydney、Alexandria
定义的结构体代码
type PFileList = ^TFileList; TFileList = Packed Record ProfileName: string; ProfileDescription: string; ProfileMemo: string; CustomURL: string; SSOProperties: string; end; type TArrOfFileList = array of TFileList;
需求说明
现有TArrOfFileList类型变量FProfileModel,需要在不逐个书写FProfileModel[0].ProfileName、FProfileModel[0].ProfileDescription这类字段名的前提下,遍历TFileList结构的所有字段,获取该记录的字段总数,用于后续类型迁移。
当前已基于UkDynamic组件实现结构体数组的文件序列化,代码如下:
procedure SaveProfile(const fs: string; var array1: TArrOfFileList); var Stream: TFileStream; begin Stream := TFileStream.Create( fs, fmCreate); try TKBDynamic.WriteTo(Stream, array1, TypeInfo(TArrOfFileList)); finally Stream.Free; end; end;
最终解决方案
参考@SilverWarior的提示,扩展UkDynamic组件的序列化逻辑,为文件存储的记录新增版本控制机制,需求已实现。
内容的提问来源于stack exchange,提问作者user14409582
相关产品推荐
相关产品推荐

