如何将处于当前状态的对象保存到文件中?这样就可以立即读取并恢复它的所有变量。
发布于 2009-03-30 19:57:54
如前所述,最简单的方法是使用流及其WriteComponent和ReadComponent方法。
但是要知道,:
你可能会找到一些你可以在这些中使用的代码,所以答案是:Replace visual component at runtime in Delphi,Duplicating components at Run-Time
发布于 2009-03-30 18:54:53
你要找的东西叫做对象持久化。这个article可能会有帮助,如果你在谷歌上搜索"delphi持久化对象“,还有很多其他的。
发布于 2009-03-30 19:28:03
如果您从TComponent继承对象,则可以使用一些内置功能将对象流式传输到文件。我认为这只适用于简单的对象。
以下是一些示例代码,可帮助您入门:
unit Unit1;
interface
uses
Classes;
type
TMyClass = class(TComponent)
private
FMyInteger: integer;
FMyBool: boolean;
FMyString: string;
public
procedure ToFile(AFileName: string);
published
property MyInteger: integer read FMyInteger write FMyInteger;
property MyString: string read FMyString write FMyString;
property MyBool: boolean read FMyBool write FMyBool;
end;
implementation
{ TMyClass }
procedure TMyClass.ToFile(AFileName: string);
var
MyStream: TFileStream;
begin
MyStream := TFileStream.Create(AFileName);
try
Mystream.WriteComponent(Self);
finally
MyStream.Free;
end;
end;
end.https://stackoverflow.com/questions/698536
复制相似问题