Delphi - Unable to save/load TObjectList to FileStream

微笑、不失礼 提交于 2019-12-11 12:27:10

问题


I have a TObjectList, which I am trying to write to disk. Although I end up with a file (54 bytes), when I change the FNAME property value to something really long, the size of the file never changes, and I get nil when I try to read it. I am at a loss as to what is wrong. Sorry for the long code snippet. it is easy to understand what is going on, just I can't figure out why it's not doing what I want.

type
  { Declare a new object type. }
  TNewObject = class(TComponent)
  private
    FName: String;
  public
    property BizName: String read FName write FName;
    constructor Create(const AName: String);
    destructor Destroy(); override;
  end;

Declare a Global var for my TObjectList

var
  Form1: TForm1;
  List: TObjectList<TNewObject>;

Declare my constructors and destructors..

constructor TNewObject.Create(const AName: String);
begin
  FName := AName;            
end;

destructor TNewObject.Destroy;
begin      
  inherited;
end;

Now add a button to create my objects...

procedure TForm1.CreateButtonClick(Sender: TObject);  
    var     
      Obj: TNewObject;
    begin
      { Create a new List. }
      { The OwnsObjects property is set by default to true -- the list will free the owned objects automatically. }
      List := TObjectList<TNewObject>.Create();

      { Add some items to the List. }
      List.Add(TNewObject.Create('One'));
      List.Add(TNewObject.Create('Two'));

      { Add a new item, but keep the reference. }
      Obj := TNewObject.Create('Three');
      List.Add(Obj);
end;

Now add a SAVE Button

procedure TForm1.SaveButtonClick(Sender: TObject);
var
  i: Integer;
  fs: TfileStream;
begin
  if SaveDialog1.Execute then
  begin
    fs := TfileStream.Create(SaveDialog1.FileName, fmCreate);
    try
      for i := 0 to List.Count - 1 do
      begin

        ShowMessage(List[i].BizName);
        fs.WriteComponent(TNewObject(List[i]));            
      end;
    finally
      fs.Free;
    end;
  end;
end;

CAVEATS: I know that only PUBLIC properties will be saved... which should be BIZNAME. The 3 entries do show up in the SHOWMESSAGE when it is being saved....

I did remember my Class Registration.

Initialization
RegisterClass(TNewObject); 

For completeness sake, here is my Load Routine as well...

procedure TForm1.LoadButtonClick(Sender: TObject);
var
  i: Integer;
  fs: TfileStream;
  vRecord: TNewObject;
begin
  if OpenDialog1.Execute then
  begin
    List.Clear; // clear list
    fs := TfileStream.Create(OpenDialog1.FileName, fmopenRead);
    try

      while fs.Position < fs.size do
      begin
        vRecord := TNewObject(fs.ReadComponent(nil));
        ShowMessage(vRecord.FName);           
        List.Add(vRecord);
      end;
    finally
      fs.Free;
    end;
    ShowMessage(IntToStr(List.Count));
  end;
end;

Thank you for your help.


回答1:


Component streaming system only streams published properties, you need to publish 'BizName'.

Alternatively you can override DefineProperties to decide what else to stream.

type
  TNewObject = class(TComponent)
  private
    FName: String;
    procedure ReadName(Reader: TReader);
    procedure WriteName(Writer: TWriter);
  protected
    procedure DefineProperties(Filer: TFiler); override;
  public
    property BizName: String read FName write FName;
    ...

procedure TNewObject.ReadName(Reader: TReader);
begin
  FName := Reader.ReadString;
end;

procedure TNewObject.WriteName(Writer: TWriter);
begin
  Writer.WriteString(FName);
end;

procedure TNewObject.DefineProperties(Filer: TFiler);
begin
  inherited;
  Filer.DefineProperty('bizname', ReadName, WriteName, FName <> '');
end;


来源:https://stackoverflow.com/questions/26835370/delphi-unable-to-save-load-tobjectlist-to-filestream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!