惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园_首页
博客园 - 司徒正美
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
Check Point Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Hugging Face - Blog
Hugging Face - Blog
B
Blog RSS Feed
阮一峰的网络日志
阮一峰的网络日志
D
DataBreaches.Net
The GitHub Blog
The GitHub Blog
G
Google Developers Blog
L
LangChain Blog
T
The Blog of Author Tim Ferriss
博客园 - 【当耐特】
Engineering at Meta
Engineering at Meta
Google DeepMind News
Google DeepMind News
雷峰网
雷峰网
量子位
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
I
InfoQ

博客园 - delphi中间件

postgresql建库建表脚本 ubuntu配置postgresql远程访问 ubuntu安装postgresql数据库 delphi 面向模型编程 array of TVarRec core.recordModel.pas DDD建模指导 rabbitMQ VS mqtt redis流的应用场景 redis消费者组 redis流的操作命令 领域服务与领域事件 业务规则和模型 限界上下文与统一语言 领域驱动 mqtt即时通讯 ActiveRecord ORM RAD(速成应用开发) unigui插件框架 工厂流水线式自动生产UNIGUI WEB软件 delphi cs\web一种统一的界面风格 - delphi中间件 - 博客园 动态生成unidbgrid 单据工厂 用json元数据填充模板 mormot2 ORM rest vs jsonrpc SSE技术详解:使用 HTTP 做服务端数据推送应用的技术 http持久连接 json-rpc 2.0 MCP服务器
使用泛型序列结构体
delphi中间件 · 2026-08-21 · via 博客园 - delphi中间件

使用泛型序列结构体

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.