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

推荐订阅源

MyScale Blog
MyScale Blog
Microsoft Azure Blog
Microsoft Azure Blog
H
Help Net Security
N
News and Events Feed by Topic
Recent Announcements
Recent Announcements
D
Docker
M
MIT News - Artificial intelligence
L
LangChain Blog
I
InfoQ
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Proofpoint News Feed
博客园_首页
MongoDB | Blog
MongoDB | Blog
美团技术团队
S
Schneier on Security
G
GRAHAM CLULEY
月光博客
月光博客
有赞技术团队
有赞技术团队
Vercel News
Vercel News
Scott Helme
Scott Helme
P
Privacy International News Feed
Last Week in AI
Last Week in AI
Recorded Future
Recorded Future
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
Attack and Defense Labs
Attack and Defense Labs
Google Online Security Blog
Google Online Security Blog
Simon Willison's Weblog
Simon Willison's Weblog
量子位
S
Security @ Cisco Blogs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
V
Visual Studio Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
NISL@THU
NISL@THU
N
Netflix TechBlog - Medium
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Spread Privacy
Spread Privacy
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
小众软件
小众软件
罗磊的独立博客
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Threatpost
L
Lohrmann on Cybersecurity
www.infosecurity-magazine.com
www.infosecurity-magazine.com
S
Security Affairs
Cloudbric
Cloudbric
爱范儿
爱范儿
H
Heimdal Security Blog
PCI Perspectives
PCI Perspectives

博客园 - Charltsing

C# 预处理器指令语言 - 完整语法速查 LoggerMessage:编写高性能的 .NET 日志 PDFImageViewer v2.1 PDF查看图像和无损导出工具,一键转换单图PDF为JBig2黑白PDF。兼谈PDF内嵌图像。 PDF的色彩空间 25 个使用.NET 10 的性能技巧 PDFontFixer v1.5 免费版,解决PDF文档复制粘贴乱码的问题,修复PDF文档字体的Unicode映射。 为什么 PDF 编辑这么难? Unicode、UTF-8、UTF-16 如何直接编辑Github的Readme.md文件 自动导入工程项目属性,Directory.Build.props 和 Directory.Build.targets 新版 C# 高效率编程指南 C# 内联数组(Inline Array):高性能数组的新选择 如何在 .NET 中使用 SIMD .NET异步编程进阶:从语法糖到高性能架构的核心突破 C# 中的安全零拷贝 使用UnsafeAccessor 访问私有字段 C# 12与.NET 8实战指南:20个提升代码质量的最佳实践 ECMA-335 CLI 规范附录 C#中避免GC压力和提高性能的8种技术 函数内联 硬件内在函数 官方:oPDF v2.1免费版,专业的PDF水印分析处理工具,无损去除水印,通杀八类PDF水印。它是PDFCommander 万能PDF水印删除工具的升级版。 C#自动引用Debug | Release版本的dll
C# Net9的模块初始化器(Module Initializer)
Charltsing · 2025-10-03 · via 博客园 - Charltsing

Module Initializer 是为了让库/框架在程序集加载时,以 “CLR 保证的、只运行一次的、不依赖类型访问的” 方式执行初始化逻辑,从而避免静态构造函数的副作用和性能问题。

为什么需要 Module Initializer?

1. 静态构造函数的问题

  • 触发时机不确定:CLR 保证在第一次访问类型前调用静态构造函数,但 你无法精确控制它什么时候运行。
  • 性能开销:CLR 对静态构造函数的类型会加锁,防止并发初始化,这会带来性能损耗。
  • 不能跨类型共享初始化逻辑:每个有静态构造函数的类都要单独处理,无法集中初始化。

2. 模块初始化器的优势

  • 只运行一次:在程序集加载时 由 CLR 自动调用一次,不依赖任何类型访问。
  • 无类型访问开销:不需要触发某个类型的静态构造函数来“顺便”初始化。
  • AOT 兼容的初始化逻逻辑
using System.Runtime.CompilerServices;

class Program
{
    static void Main()
    {
        Console.WriteLine("Main");
    }
}

class Init
{
    [ModuleInitializer]
    public static void Initialize()
    {
        Console.WriteLine("Module Initializer runs before Main!");
    }
}

输出:

Module Initializer runs before Main!
Main
 

在 NativeAOT 场景里,所有必须在运行时“反射”才能完成的事情都必须提前在编译期做完。Module Initializer 就是“把编译期算好的东西在程序一启动就塞进运行时”的唯一可靠入口——它跑在 任何用户代码、任何泛型实例化、任何反射调用之前,而且 不需要触发某个类型的静态构造,因此不会引入 AOT 禁止的动态路径。

NativeAOT 的底线是 “运行时不能做任何‘发现’工作”。所有发现必须在编译期完成,而 发现结果塞进运行时的唯一零成本窗口就是 Module Initializer;
因此只要你在 AOT 模式下看到“编译期源生成器 + 运行时注册表/函数指针/缓存” 这种组合,背后几乎一定藏着一个 [ModuleInitializer]——它已经成了 AOT 生态的隐形基础设施。