为什么自定义对象持久化失败 ( 积分: 100 )

  • 主题发起人 主题发起人 sunjunfeng3
  • 开始时间 开始时间
S

sunjunfeng3

Unregistered / Unconfirmed
GUEST, unregistred user!
我自定义了一个对象,把他的实例持久化到硬盘,然后再读回来的时候参数去选择了
type
TMyClass = class(TComponent) //自定义了一个对象
public
aaa:string;
end;
//持久化到硬盘文件
var
my:TMyClass;
fs: TFileStream;
begin
my := TMyClass.Create(self);
my.aaa := "aaaaaaaaaaaaaaaaaaaaaaaaa ";
fs := TFileStream.Create( "c:/test.txt ", fmCreate);
my := TMyClass.Create(self);
fs.WriteComponent(my );
fs.free();
end;
//再读回来
var
fs: TFileStream;
my1,my2:TMyClass;
begin
fs := TFileStream.Create( "c:/test.txt ", fmOpenRead);
ooo := fs.ReadComponent(my2);
my1 := TMyClass(ooo);
ShowMessage(my1.aaa); //这时候my1.aaa为空,为什么是空呢,上面我已经赋值了
end;
 
试试这样写
TMyClass = class(TComponent) //自定义了一个对象
private
Faaa: string;
published
property aaa:string read Faaa write Faaa;
end;
只有Published的属性才能被Delphi持久化,保存下来
 
Published的属性能够持久化,如果是非published的属性也是可以的,不过麻烦点,需要override基类的DefineProperties方法,看delphi帮助:Designates methods for storing an object's unpublished data on a stream such as a form file.

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

{只有Published的属性才能被Delphi持久化,保存下来}
type
TMyClass = class(TComponent)
private
aaa: string;
function getsvalue: string;
procedure setsvalue(const sve: string);
procedure storeStringProperty(writer: TWriter);
procedure loadStringProperty(reader: TReader);
protected
procedure DefineProperties(filer: TFiler);override;
end;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Memo1: TMemo;

procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
aMyClass: TMyClass;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
fs: TMemoryStream;
begin
aMyClass.setsvalue('aaaaaaaaaa');
fs := TMemoryStream.Create;
fs.WriteComponent(aMyClass);
fs.SaveToFile('test.txt');
fs.free;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
fs: TMemoryStream;
begin
fs := TMemoryStream.Create;
fs.LoadFromFile('test.txt');
fs.ReadComponent(aMyClass);
ShowMessage(aMyClass.getsvalue);
fs.Free;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
aMyClass:= TMyClass.Create(self);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
aMyClass.Free;
end;


{ TMyClass }

procedure TMyClass.DefineProperties(filer: TFiler);
begin
inherited;
filer.DefineProperty('mystringproperty',loadStringProperty,storeStringProperty,true);
end;

function TMyClass.getsvalue: string;
begin
Result:= aaa;
end;

procedure TMyClass.loadStringProperty(reader: TReader);
begin
if reader.ReadBoolean then
aaa:= reader.ReadString;
end;

procedure TMyClass.setsvalue(const sve: string);
begin
aaa:= sve;
end;

procedure TMyClass.storeStringProperty(writer: TWriter);
begin
writer.WriteBoolean(aaa<> '');
if aaa<>'' then
writer.WriteString(aaa);
end;

end.
 
关键是下面三个procedure
procedure storeStringProperty(writer: TWriter);
procedure loadStringProperty(reader: TReader);
procedure DefineProperties(filer: TFiler);override;

也可以这么调用aMyClass.setsvalue('aaaaaaaaaa');
替换为aMyClass.aaa:= 'aaaaaaaaaaa';
同理,showmessage(aMyClass.aaa);
 
但是是不是这种情况只能处理简单类型的情况 ,如果有自己定义的复杂类型,比如自定义一个类型 MyType做为属性的类型,那么这个类型也就无法被持久化.要对复杂类型持久化应该怎么做呢?.net可以直接序列化自定义的class,DELPHI应该怎么做?
type
MyType = class(TObject)
myValue:String;
end;
 
后退
顶部