T2FGame.CodeGen.Action
1.0.9
dotnet add package T2FGame.CodeGen.Action --version 1.0.9
NuGet\Install-Package T2FGame.CodeGen.Action -Version 1.0.9
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="T2FGame.CodeGen.Action" Version="1.0.9"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="T2FGame.CodeGen.Action" Version="1.0.9" />
<PackageReference Include="T2FGame.CodeGen.Action"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add T2FGame.CodeGen.Action --version 1.0.9
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: T2FGame.CodeGen.Action, 1.0.9"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package T2FGame.CodeGen.Action@1.0.9
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=T2FGame.CodeGen.Action&version=1.0.9
#tool nuget:?package=T2FGame.CodeGen.Action&version=1.0.9
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
T2FGame.CodeGen.Action
T2FGame Action 模块的 Source Generator,在编译时生成 Action 调用器和注册表,消除运行时反射。
功能
- 零反射方法调用:编译时生成直接方法调用代码,替代
MethodInfo.Invoke() - 编译时元数据注册:消除启动时的程序集扫描
- AOT 友好:生成的代码完全支持 Native AOT 编译
- 类型安全:编译时验证所有 Action 方法签名
性能对比
| 操作 | 反射方式 | Source Generator |
|---|---|---|
| 启动扫描 | ~50-100ms | 0ms (编译时完成) |
| 方法调用 | ~500ns | ~10ns (直接调用) |
| 异步结果提取 | ~200ns (多次反射) | ~5ns (泛型方法) |
| 特性查询 | ~100ns | 0ns (预生成常量) |
使用方法
1. 添加项目引用
在使用 Action 的项目中添加生成器引用:
<ItemGroup>
<ProjectReference Include="..\T2FGame.Action\T2FGame.Action.csproj" />
<ProjectReference Include="..\T2FGame.CodeGen.Action\T2FGame.CodeGen.Action.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>
2. 编写 Action 控制器
[ActionController(1, "用户模块")]
public class UserController
{
[ActionMethod(1, description: "用户登录")]
public async Task<LoginResponse> LoginAsync(LoginRequest request, FlowContext context)
{
// 业务逻辑
}
[ActionMethod(2, requireAuth: false, description: "用户注册")]
public async ValueTask<RegisterResponse> RegisterAsync(RegisterRequest request, FlowContext context)
{
// 业务逻辑
}
}
3. 使用生成的注册方法
// 旧方式(运行时反射)
services.AddT2FGameAction(typeof(UserController).Assembly);
// 新方式(编译时生成,零反射)
services.AddT2FGameActionGenerated();
生成的代码
编译后会自动生成以下文件:
ActionInvoker.g.cs
public sealed class GeneratedActionInvoker : IGeneratedActionInvoker
{
public async ValueTask<byte[]?> InvokeAsync(
int cmdMerge, object controller, object? request, FlowContext context)
{
return cmdMerge switch
{
// [1:1] UserController.LoginAsync
0x00010001 => await HandleTaskOfTAsync<LoginResponse>(
((UserController)controller).LoginAsync((LoginRequest)request!, context)),
// [1:2] UserController.RegisterAsync
0x00010002 => await HandleValueTaskOfTAsync<RegisterResponse>(
((UserController)controller).RegisterAsync((RegisterRequest)request!, context)),
_ => throw new InvalidOperationException($"未知命令: 0x{cmdMerge:X8}")
};
}
}
ActionRegistry.g.cs
public sealed class GeneratedActionRegistry
{
private static readonly FrozenDictionary<int, ActionMethodInfo> _methods;
static GeneratedActionRegistry()
{
var methods = new Dictionary<int, ActionMethodInfo>
{
[0x00010001] = new ActionMethodInfo { /* ... */ },
[0x00010002] = new ActionMethodInfo { /* ... */ },
};
_methods = methods.ToFrozenDictionary();
}
}
架构
编译时:
用户代码 [ActionController] → ActionSourceGenerator → 生成 .g.cs 文件
运行时:
请求 → Pipeline → GeneratedActionInvoker.InvokeAsync → 直接方法调用
↓
switch (cmdMerge)
{
case 0x00010001:
return controller.LoginAsync(req, ctx);
...
}
与现有代码兼容
生成的代码完全兼容现有的 ActionMethodRegistry 和 Pipeline 处理器:
GeneratedActionRegistry实现与ActionMethodRegistry相同的接口IGeneratedActionInvoker可以注入到自定义 Handler 中使用AddT2FGameActionGenerated()同时注册兼容的ActionMethodRegistry
许可证
MIT License
There are no supported framework assets in this package.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Microsoft.Bcl.HashCode (>= 1.1.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.