GFramework/GFramework.SourceGenerators
GeWuYou 796408539e refactor(generators): 优化ContextGetGenerator代码结构并改进异常处理
- 修改GetService方法文档,将返回值描述从"返回null"改为"抛出异常"
- 为GetService方法添加InvalidOperationException异常说明
- 删除冗余的IsExternalInit类文件
- 重构属性匹配逻辑,使用预定义集合进行候选属性名称验证
- 添加辅助方法HasCandidateAttribute、TryGetAttributeSimpleName等提升代码可读性
- 改进集合类型推断逻辑,支持接口类型的遍历匹配
- 更新单元测试以验证完全限定名属性和只读列表类型的支持
- 修正诊断错误位置信息的准确性
2026-03-28 12:54:17 +08:00
..

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 调用;已显式标记字段的优先级更高。