GeWuYou 2ae783c127 feat(generator): 添加对 [GetAll] 特性的静态和只读字段跳过支持
- 添加了新的诊断规则 GF_ContextGet_007 和 GF_ContextGet_08
- 实现了对静态字段和只读字段的跳过逻辑
- 为 [GetAll] 特性添加了跳过字段的警告提示
- 更新了测试用例验证跳过逻辑的正确性
- 修改了代码生成顺序以确保正确的绑定推断
- 在 README 中添加了关于字段跳过的文档说明
2026-03-30 08:40:57 +08:00

53 lines
1.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# GFramework.SourceGenerators
Core 侧通用源码生成器模块。
## Context Get 注入
当类本身是上下文感知类型时,可以通过字段特性生成一个手动调用的注入方法:
- `[GetService]`
- `[GetServices]`
- `[GetSystem]`
- `[GetSystems]`
- `[GetModel]`
- `[GetModels]`
- `[GetUtility]`
- `[GetUtilities]`
- `[GetAll]`
上下文感知类满足以下任一条件即可:
- 类上带有 `[ContextAware]`
- 继承 `ContextAwareBase`
- 实现 `IContextAware`
生成器会生成 `__InjectContextBindings_Generated()`,需要在合适的生命周期中手动调用。在 Godot 中通常放在 `_Ready()`
```csharp
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]` 作用于类本身,会自动扫描字段并推断 `Model``System``Utility` 相关的 `GetX` 调用;已显式标记字段的优先级更高。
`Service``Services` 绑定不会在 `[GetAll]` 下自动推断。对于普通引用类型字段,请显式使用 `[GetService]`
`[GetServices]`,避免将非上下文服务字段误判为服务依赖。
`[GetAll]` 会跳过 `const``static``readonly` 字段。若某个字段本来会被 `[GetAll]` 推断为
`Model``System``Utility` 绑定,但因为字段不可赋值而被跳过,生成器会发出警告提示该字段不会参与生成。