unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, System.ComponentModel, Borland.Vcl.StdCtrls,
System.Runtime.Serialization.Formatters.Binary;
type
[Serializable()]
TMyRec =
packed record
FValue1 : Double;
FValue2 :
String;
end;
[Serializable()]
TData =
class
private
FValue1,
FValue2:
String;
FRec: TMyRec;
[NonSerialized()] FValue3:
String;
end;
TForm1 =
class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.nfm}
procedure TForm1.Button1Click(Sender: TObject);
var aData: TData;
aRec: TMyRec;
aFileStream: TFileStream;
aFormatter: BinaryFormatter;
begin
aData := TData.Create;
aData.FValue1 := '
Wert 1';
aData.FValue2 := '
Wert 2';
aData.FValue3 := '
Wert 3';
aData.FRec.FValue1 := 0.123;
aData.FRec.FValue2 := '
abc';
aRec.FValue1 := 0.123;
aRec.FValue2 := '
abc';
aFileStream := TFileStream.Create('
test.dat', fmCreate);
aFormatter := BinaryFormatter.Create;
aFormatter.Serialize(aFileStream, aData);
aFileStream := TFileStream.Create('
test2.dat', fmCreate);
aFormatter := BinaryFormatter.Create;
aFormatter.Serialize(aFileStream, aRec);
end;
procedure TForm1.Button2Click(Sender: TObject);
var aData: TData;
aRec: TMyRec;
aFileStream: TFileStream;
aFormatter: BinaryFormatter;
begin
aFileStream := TFileStream.Create('
test.dat', fmOpenRead);
aFormatter := BinaryFormatter.Create;
aData := TData(aFormatter.Deserialize(aFileStream));
ShowMessage(aData.FValue1);
ShowMessage(aData.FValue2);
ShowMessage(aData.FValue3);
aFileStream := TFileStream.Create('
test2.dat', fmOpenRead);
aFormatter := BinaryFormatter.Create;
aRec := TMyRec(aFormatter.Deserialize(aFileStream));
ShowMessage(aRec.FValue1.ToString);
ShowMessage(aRec.FValue2);
end;
end.