保存/加载 TObject(TPercient) 到 XML

发布于 2024-11-15 01:45:57 字数 705 浏览 4 评论 0原文

大家。

我正在尝试

TA= class(TPersistent)
private
    FItems: TObjectList<TB>;

    FOnChanged: TNotifyEvent;
public
    constructor Create;
    destructor Destroy; override;
    ...
    procedure Delete(Index: Integer);
    procedure Clear;
    procedure SaveToFile(const FileName: string);
    ...
    property OnChanged: TNotifyEvent read FOnChanged write FOnChanged;
end;

使用以下代码将我的类保存到文件:

var
  Storage: TJvAppXMLFileStorage;
begin
  Storage := TJvAppXMLFileStorage.Create(nil);
  try
    Storage.WritePersistent('', Self);
    Storage.Xml.SaveToFile(FileName);
  finally
    Storage.Free;
  end;

但文件始终为空。

我做错了什么?

everybody.

I'm trying to save my class:

TA= class(TPersistent)
private
    FItems: TObjectList<TB>;

    FOnChanged: TNotifyEvent;
public
    constructor Create;
    destructor Destroy; override;
    ...
    procedure Delete(Index: Integer);
    procedure Clear;
    procedure SaveToFile(const FileName: string);
    ...
    property OnChanged: TNotifyEvent read FOnChanged write FOnChanged;
end;

to file using the following code:

var
  Storage: TJvAppXMLFileStorage;
begin
  Storage := TJvAppXMLFileStorage.Create(nil);
  try
    Storage.WritePersistent('', Self);
    Storage.Xml.SaveToFile(FileName);
  finally
    Storage.Free;
  end;

but file is always empty.

What am I doing the wrong way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒解孤独 2024-11-22 01:45:57

看起来 TJvCustomAppStorage 不支持属性中的泛型。该代码没有使用扩展 RTTI,并且对 TJvCustomAppStorage.GetPropCount 的调用返回 0。

这导致了另一个问题 - 是否有支持泛型的 Delphi 对象序列化库?

我的测试代码:

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils, Classes, Generics.Collections, JvAppXmlStorage;
type

  TA = class(TPersistent)
  private
    FItems: TObjectList<TPersistent>;
  public
    constructor Create;
  published
    property
      Items: TObjectList < TPersistent > read FItems write FItems;
  end;

  { TA }

constructor TA.Create;
begin
  FItems := TObjectList<TPersistent>.Create;
end;

var
  Storage: TJvAppXMLFileStorage;
  Test: TA;
begin
  Test := TA.Create;

  Test.Items.Add(TPersistent.Create);

  Storage := TJvAppXMLFileStorage.Create(nil);
  try
    Storage.WritePersistent('', Test);
    WriteLn(Storage.Xml.SaveToString);
    ReadLn;
  finally
    Storage.Free;
  end;

end.

It looks like TJvCustomAppStorage does not support Generics in properties. The code makes no use of extended RTTI and the call to TJvCustomAppStorage.GetPropCount returns 0.

This leads to another question - Are there Delphi object serialization libraries with support for Generics??

My test code:

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils, Classes, Generics.Collections, JvAppXmlStorage;
type

  TA = class(TPersistent)
  private
    FItems: TObjectList<TPersistent>;
  public
    constructor Create;
  published
    property
      Items: TObjectList < TPersistent > read FItems write FItems;
  end;

  { TA }

constructor TA.Create;
begin
  FItems := TObjectList<TPersistent>.Create;
end;

var
  Storage: TJvAppXMLFileStorage;
  Test: TA;
begin
  Test := TA.Create;

  Test.Items.Add(TPersistent.Create);

  Storage := TJvAppXMLFileStorage.Create(nil);
  try
    Storage.WritePersistent('', Test);
    WriteLn(Storage.Xml.SaveToString);
    ReadLn;
  finally
    Storage.Free;
  end;

end.
攒眉千度 2024-11-22 01:45:57

我不确定,但如果 TJvAppXMLFileStorage 使用 RTTI 那么我认为您必须发布要保存/加载的属性。

I'm not sure but if TJvAppXMLFileStorage uses RTTI then I think you have to publish the properties that you want to save / load.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文