












使用泛型序列结构体
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls; type Trecord1 = packed record f1: Integer; f2: Integer; end; RecordSerial<T> = record class function marshal(rec: T): TBytes; static; //序列 class function unmarshal(buf: TBytes): T; static; //还原 end; TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} class function RecordSerial<T>.marshal(rec: T): TBytes; begin var len: Integer := SizeOf(rec); SetLength(result, len); Move(rec, Result[0], len); end; class function RecordSerial<T>.unmarshal(buf: TBytes): T; begin var len: Integer := Length(buf); Move(buf[0], Result, len); end; var bs: TBytes; procedure TForm1.Button1Click(Sender: TObject); begin //序列 var r1: Trecord1; r1.f1 := 99; bs := RecordSerial<Trecord1>.marshal(r1); //还原 var r2: Trecord1 := RecordSerial<Trecord1>.unmarshal(bs); ShowMessage(r2.f1.ToString); //99 end; end.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。