LayerBase.Generator
1.4.5
See the version list below for details.
dotnet add package LayerBase.Generator --version 1.4.5
NuGet\Install-Package LayerBase.Generator -Version 1.4.5
<PackageReference Include="LayerBase.Generator" Version="1.4.5" />
<PackageVersion Include="LayerBase.Generator" Version="1.4.5" />
<PackageReference Include="LayerBase.Generator" />
paket add LayerBase.Generator --version 1.4.5
#r "nuget: LayerBase.Generator, 1.4.5"
#:package LayerBase.Generator@1.4.5
#addin nuget:?package=LayerBase.Generator&version=1.4.5
#tool nuget:?package=LayerBase.Generator&version=1.4.5
🚀 LayerBase.Generator: 工业级架构源生成器
LayerBase.Generator 是 LayerBase 高性能架构总线的专属 C# 源生成器(Source Generator) 插件。
它负责将高层抽象的依赖注入(DI)和事件订阅特性,在编译期转化为直接且连续的底层委托(SOA 数组布局),从而帮助 LayerBase 实现了 零运行期反射开销和高达 1.5 亿次/秒的极限吞吐量。
🎯 核心功能与特性
通过挂载简单的特性,LayerBase.Generator 将在后台静默生成所有的样板代码:
- 自动依赖注入与层级挂载 (
[OwnerLayer])- 只需给您的
Service或切片式的EventHandler打上[OwnerLayer(typeof(YourLayer))]特性,生成器便会生成自动注册逻辑。 LayerHub.CreateLayers().Build()能够全自动扫描并在后台完成服务依赖、实例创建与层级挂载。
- 只需给您的
- 事件总线零反射绑定 (
[SubscribeFlow],[SubscribeAsync])- 不再需要手动维护繁琐的
EventBus.SubscribeFlow<T>(Method)。 - 只需在您的
Manager(继承ILayerContext) 的事件处理方法上挂载[SubscribeFlow]或[SubscribeAsync]。 - 源生成器会直接提取函数的底层委托,生成高密度的包装类并注入全局总线,彻底消除运行时反射查找。
- 不再需要手动维护繁琐的
- 全局异常与元数据观察 (
EventMetaData<T>)- 对于网络同步包或核心状态事件,只需定义一个继承自
EventMetaData<T>的类。 - 生成器会自动将它与
partial struct事件绑定,建立一个全局级别的、零侵入的异常拦截点 ,任何对该事件处理所产生的未捕获异常都会流向这里进行统一监控。
- 对于网络同步包或核心状态事件,只需定义一个继承自
📦 如何安装与配置
由于本包属于编译期分析器(Analyzer),在引入它时请务必配置引用类型,以防将其作为运行库打包进项目中。
- NuGet 快速安装:
dotnet add package LayerBase.Generator --version 1.3.3 - 正确配置 .csproj 引用:
当您在项目中使用该生成器时,确保它的
OutputItemType设置为Analyzer,且不输出运行时程序集:
(如果您使用的是本地源码依赖,配置方法类似):<ItemGroup> <PackageReference Include="LayerBase.Generator" Version="1.3.3" OutputItemType="Analyzer" ReferenceOutputAssembly="false" /> </ItemGroup><ItemGroup> <ProjectReference Include="LayerBase.Generator\LayerBase.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" /> </ItemGroup>
⚙️ 常见问题 (FAQ)
为什么提示“事件类型必须是 partial 的”?
当您使用 EventMetaData<T> 为事件绑定全局异常观察者时,生成器需要向该结构体中注入静态代码,以完成无反射的底层注册。因此,您的事件必须像这样声明:
public partial struct PlayerDeadEvent { ... }
为什么我的 [SubscribeFlow] 报错或没有生效?
- 请确保您的类继承了
ILayerContext接口(通常是各种Manager)。 - 请确保您的类被标记为
partial,因为生成器需要在该类的同名分部中写入Initialize接口的实现逻辑。 - 请确保您的处理方法的参数前面使用了
in修饰符(如in DamageEvent e),以享受结构体的零分配传递。
🔗 关于 LayerBase
LayerBase.Generator 是 LayerBase 极限性能生态的最后一块拼图。结合 LBTask 的零分配异步和底层的 SOA 零分支引擎,构建您的工业级架构。
项目主页:LayerBase GitHub 仓库
🚀 LayerBase.Generator: Industrial-Grade Architecture Source Generator
LayerBase.Generator is the exclusive C# Source Generator plugin for the LayerBase High-Performance Architecture Bus.
It is responsible for transforming high-level abstractions like Dependency Injection (DI) and event subscription attributes into direct, contiguous low-level delegates (SOA Array Layout) during compile-time. This enables LayerBase to achieve zero runtime reflection overhead and extreme throughput of up to 150 million ops/sec.
🎯 Core Features & Highlights
By attaching simple attributes, LayerBase.Generator silently generates all the boilerplate code in the background:
- Automatic Dependency Injection & Layer Mounting (
[OwnerLayer])- Simply tag your
Serviceor slice-basedEventHandlerwith the[OwnerLayer(typeof(YourLayer))]attribute, and the generator will produce the auto-registration logic. LayerHub.CreateLayers().Build()can fully automatically scan and complete service dependency resolution, instance creation, and layer mounting in the background.
- Simply tag your
- Zero-Reflection Event Bus Binding (
[SubscribeFlow],[SubscribeAsync])- No more manual maintenance of tedious
EventBus.SubscribeFlow<T>(Method)calls. - Just attach
[SubscribeFlow]or[SubscribeAsync]to the event handling methods in yourManager(inheriting fromILayerContext). - The Source Generator extracts the underlying delegates directly, generates high-density wrapper classes, and injects them into the global bus, completely eliminating runtime reflection lookups.
- No more manual maintenance of tedious
- Global Exception & MetaData Observation (
EventMetaData<T>)- For network sync packets or core state events, simply define a class inheriting from
EventMetaData<T>. - The generator automatically binds it to your
partial structevent, establishing a global-level, non-intrusive exception interception point. Any unhandled exceptions generated during the processing of that event will flow here for unified monitoring.
- For network sync packets or core state events, simply define a class inheriting from
📦 Installation & Configuration
Since this package is a compile-time analyzer, it must be configured as an analyzer reference to prevent it from being bundled into your project as a runtime library.
- Quick Install via NuGet:
dotnet add package LayerBase.Generator --version 1.3.3 - Correct .csproj Reference Configuration:
When using this generator in your project, ensure its
OutputItemTypeis set toAnalyzerand that it does not output a runtime assembly:
(If you are using a local source dependency, the configuration is similar):<ItemGroup> <PackageReference Include="LayerBase.Generator" Version="1.3.3" OutputItemType="Analyzer" ReferenceOutputAssembly="false" /> </ItemGroup><ItemGroup> <ProjectReference Include="LayerBase.Generator\LayerBase.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" /> </ItemGroup>
⚙️ FAQ
Why does it say "Event type must be partial"?
When using EventMetaData<T> to bind a global exception observer to an event, the generator needs to inject static code
into the struct to complete the reflection-free low-level registration. Therefore, your event must be declared like
this:
public partial struct PlayerDeadEvent { ... }
Why is my [SubscribeFlow] erroring or not taking effect?
- Ensure your class inherits the
ILayerContextinterface (typically your variousManagers). - Ensure your class is marked as
partial, as the generator needs to write the implementation logic for theInitializeinterface in a partial part of that class. - Ensure your handling method's parameter uses the
inmodifier (e.g.,in DamageEvent e) to enjoy zero-allocation passing of structs.
🔗 About LayerBase
LayerBase.Generator is the final piece of the LayerBase extreme performance ecosystem. Combine it with LBTask for
zero-allocation async and the low-level SOA branchless engine to build your industrial-grade architecture.
Project Homepage: LayerBase GitHub Repository
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 4.3.0)
- Microsoft.CodeAnalysis.CSharp.Workspaces (>= 4.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.5.1 | 86 | 5/16/2026 |
| 1.5.0 | 90 | 5/14/2026 |
| 1.4.9 | 100 | 5/12/2026 |
| 1.4.8.2 | 98 | 5/11/2026 |
| 1.4.7.5 | 86 | 5/5/2026 |
| 1.4.7.4 | 79 | 5/5/2026 |
| 1.4.7.3 | 84 | 5/4/2026 |
| 1.4.7.1 | 90 | 5/3/2026 |
| 1.4.5 | 89 | 5/2/2026 |
| 1.4.3 | 94 | 4/30/2026 |
| 1.4.2 | 92 | 4/28/2026 |
| 1.4.1 | 97 | 4/25/2026 |
| 1.4.0 | 88 | 4/24/2026 |
| 1.3.6 | 87 | 4/21/2026 |
| 1.3.4 | 90 | 4/20/2026 |
| 1.3.3 | 90 | 4/19/2026 |
| 1.3.2 | 87 | 4/19/2026 |
| 1.3.1 | 89 | 4/19/2026 |
| 1.3.0 | 87 | 4/19/2026 |
| 1.2.0 | 98 | 4/18/2026 |