如何序列化Delphi TObjectList< TMyClass>用TJvAppXMLFileStorage键入XML? [英] How to serialize Delphi TObjectList<TMyClass> type to XML with TJvAppXMLFileStorage?

查看:366
本文介绍了如何序列化Delphi TObjectList< TMyClass>用TJvAppXMLFileStorage键入XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前,为了保存某些应用程序的设置,我使用了:

Previously, to save the settings of some applications, I used:


  • A TSettings = class )

  • 要发布的属性序列化的每个数据

  • 对象列表是 TCollection TCollectionItem 类型

  • 使用单行保存所有内容的 TJvAppXMLFileStorage 组件:

JvAppXMLFileStorage.WritePersistent(...);

JvAppXMLFileStorage.WritePersistent(...);

但是现在,我正在使用TObjectList作为TSettings类中的属性。

所以我放弃TCollection / TCollectionItem以支持泛型 ...

当序列化它时,没有项目列表 ...我认为这是因为TObjectList不是来自TPersistent。

BUT now, I'm using TObjectList as properties in the TSettings class.
So I drop the TCollection/TCollectionItem in favor of Generics ...
When serializing it, there is no list of items ... I think it's because TObjectList is not from TPersistent.

如何我使用 TJvAppXMLFileStorage 序列化我的 TObjectList

How can I serialize my TObjectList<> with TJvAppXMLFileStorage ?

推荐答案

我通过调用 JvAppXMLFileStorage.WriteList ,成功地序列化了几行代码的通用列表。

I've successfuly serialize my generic list with few lines of code by calling JvAppXMLFileStorage.WriteList.

首先,这是我序列化列表的方式。 WriteGenericsObjectListItem< TMyClass> 方法详细如下。

First, this is how I serialized the list. The WriteGenericsObjectListItem<TMyClass> method is detailed below.

JvAppXMLFileStorage.WriteList('mylist',TObject(MyGenericList), MyGenericList.Count, WriteGenericsObjectListItem<TMyClass>);

然后,我只需要定义如何序列化每个项目通用列表。为此,我创建了一个通用方法:

Then, I just need to define how to serialize each item of the generic list. For this, I've created a generic method:

procedure TMySerializer.WriteGenericsObjectListItem<T>(Sender: TJvCustomAppStorage;
  const Path: string; const List: TObject; const Index: Integer; const ItemName: string);
begin
  if(List is TObjectList<T>) then
    if Assigned(TObjectList<T>(List)[Index]) then
      Sender.WritePersistent(Sender.ConcatPaths([Path, Sender.ItemNameIndexPath (ItemName, Index)]), TPersistent(TObjectList<T>(List)[Index]));
end;

就是这样!

我没有修改JCL / JVCL代码,只有将这些添加到我的程序。

我想我将提交一个补丁到JCL / JVCL团队来添加与所有泛型容器的兼容性。

That's it!
I haven't modify JCL/JVCL code, only add these to my program.
I think I will submit a patch to JCL/JVCL team to add the compatibility with all Generics containers.

I希望这可以帮助你!

I hope this can help you !

这篇关于如何序列化Delphi TObjectList&lt; TMyClass&gt;用TJvAppXMLFileStorage键入XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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