GeWuYou 78af119f38 feat(generator): 添加上下文感知注入源码生成器
- 移除ContextAwareServiceExtensions中GetService/GetSystem/GetModel/GetUtility方法的可空返回值
- 添加ContextGetGenerator源码生成器,支持通过特性自动生成上下文注入代码
- 新增GetService/GetServices/GetSystem/GetSystems/GetModel/GetModels/GetUtility/GetUtilities/GetAll特性
- 添加ContextGetDiagnostics提供注入相关的编译时诊断检查
- 实现INamedTypeSymbol扩展方法AreAllDeclarationsPartial用于检查partial类声明
- 添加ITypeSymbol扩展方法IsAssignableTo用于类型兼容性判断
- 创建FieldCandidateInfo和TypeCandidateInfo记录类型用于存储生成器候选信息
- 添加IsExternalInit内部类型支持低版本.NET框架的init-only setter功能
- 更新AnalyzerReleases.Unshipped.md添加新的诊断规则条目
- 创建完整的单元测试验证生成器功能和各种边界情况
2026-03-28 11:29:40 +08:00

1.1 KiB
Raw Blame History

GFramework.SourceGenerators

Core 侧通用源码生成器模块。

Context Get 注入

当类本身是上下文感知类型时,可以通过字段特性生成一个手动调用的注入方法:

  • [GetService]
  • [GetServices]
  • [GetSystem]
  • [GetSystems]
  • [GetModel]
  • [GetModels]
  • [GetUtility]
  • [GetUtilities]
  • [GetAll]

上下文感知类满足以下任一条件即可:

  • 类上带有 [ContextAware]
  • 继承 ContextAwareBase
  • 实现 IContextAware

生成器会生成 __InjectContextBindings_Generated(),需要在合适的生命周期中手动调用。在 Godot 中通常放在 _Ready()

using GFramework.SourceGenerators.Abstractions.Rule;

[ContextAware]
public partial class InventoryPanel
{
    [GetModel]
    private IInventoryModel _inventory = null!;

    [GetServices]
    private IReadOnlyList<IInventoryStrategy> _strategies = null!;

    public override void _Ready()
    {
        __InjectContextBindings_Generated();
    }
}

[GetAll] 作用于类本身,会自动扫描字段并推断对应的 GetX 调用;已显式标记字段的优先级更高。