gewuyou b96565ffa3 docs(documentation): 扩展项目功能文档覆盖
- 更新 meta-package 与安装入口,明确聚合范围、当前运行时基线和首页选包路径
- 补充 Game 配置工具文档与导航,把 VS Code config workflow 纳入 reader-facing 采用链路
- 修正文档与实现不一致的 source-generator 与 CQRS 契约说明,补充 support module 边界
- 记录 RP-038 批处理恢复点、委派结论和本轮验证结果
2026-04-25 17:10:37 +08:00

262 lines
5.2 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.

---
title: 枚举扩展生成器
description: 介绍枚举扩展生成器生成的 IsX 与 IsIn 方法及其典型用法。
---
# 枚举扩展生成器
> 自动为枚举类型生成扩展方法
## 概述
枚举扩展生成器为标记了 `[GenerateEnumExtensions]` 属性的枚举自动生成两种扩展方法:
1. **IsX 方法**:为每个枚举值生成判断方法
2. **IsIn 方法**:判断枚举值是否在指定集合中
## 基础使用
```csharp
using GFramework.Core.SourceGenerators.Abstractions.Enums;
[GenerateEnumExtensions]
public enum GameState
{
Normal,
Paused,
GameOver
}
```
## 生成的代码
编译器会自动生成如下扩展方法:
```csharp
// <auto-generated/>
public static class GameStateExtensions
{
// 为每个枚举值生成 IsX 方法
public static bool IsNormal(this GameState value)
=> value == GameState.Normal;
public static bool IsPaused(this GameState value)
=> value == GameState.Paused;
public static bool IsGameOver(this GameState value)
=> value == GameState.GameOver;
// 生成 IsIn 方法
public static bool IsIn(this GameState value, params GameState[] values)
{
if (values == null) return false;
foreach (var v in values) if (value == v) return true;
return false;
}
}
```
## 使用示例
```csharp
var state = GameState.Paused;
// 使用 IsX 方法
if (state.IsPaused())
{
Console.WriteLine("游戏已暂停");
}
// 使用 IsIn 方法
if (state.IsIn(GameState.Paused, GameState.GameOver))
{
Console.WriteLine("游戏未在运行中");
}
```
## 配置选项
可以通过属性参数控制生成行为:
```csharp
[GenerateEnumExtensions(
GenerateIsMethods = true, // 是否生成 IsX 方法(默认 true
GenerateIsInMethod = true // 是否生成 IsIn 方法(默认 true
)]
public enum GameState
{
Normal,
Paused,
GameOver
}
```
### 配置选项说明
#### 实际支持的选项
当前版本只支持以下配置选项:
| 参数 | 类型 | 默认值 | 说明 |
|--------------------|------|------|-------------------|
| GenerateIsMethods | bool | true | 是否为每个枚举值生成 IsX 方法 |
| GenerateIsInMethod | bool | true | 是否生成 IsIn 方法 |
### 只生成 IsX 方法
```csharp
[GenerateEnumExtensions(GenerateIsInMethod = false)]
public enum GameState
{
Normal,
Paused
}
// 只生成 IsNormal() 和 IsPaused(),不生成 IsIn()
```
### 只生成 IsIn 方法
```csharp
[GenerateEnumExtensions(GenerateIsMethods = false)]
public enum GameState
{
Normal,
Paused
}
// 只生成 IsIn(),不生成 IsNormal() 和 IsPaused()
```
## 最佳实践
### 1. 命名约定
生成的方法名基于枚举值名称:
- `Normal``IsNormal()`
- `GameOver``IsGameOver()`
- `HTTP_ERROR``IsHTTP_ERROR()`
### 2. 性能考虑
生成的方法是内联的,性能与手写代码相同:
```csharp
// 生成的代码
public static bool IsNormal(this GameState value)
=> value == GameState.Normal;
// 等价于手写
if (state == GameState.Normal) { }
```
### 3. 什么时候值得关闭某个开关
- 只想保留精确单值判断:
- 保留 `GenerateIsMethods = true`
- 关闭 `GenerateIsInMethod = false`
- 只想保留多值判断入口:
- 保留 `GenerateIsInMethod = true`
- 关闭 `GenerateIsMethods = false`
### 4. 可读性提升
```csharp
// 使用生成的方法(推荐)
if (state.IsPaused())
{
ResumeGame();
}
// 直接比较(不推荐)
if (state == GameState.Paused)
{
ResumeGame();
}
```
## 实际应用示例
### 游戏状态管理
```csharp
[GenerateEnumExtensions]
public enum GameState
{
MainMenu,
Playing,
Paused,
GameOver,
Victory
}
public class GameManager
{
private GameState _currentState = GameState.MainMenu;
public bool CanProcessInput()
{
// 使用 IsIn 方法简化多值判断
return _currentState.IsIn(GameState.Playing, GameState.MainMenu);
}
public void Update()
{
// 使用 IsX 方法提高可读性
if (_currentState.IsPlaying())
{
UpdateGameLogic();
}
else if (_currentState.IsPaused())
{
UpdatePauseMenu();
}
}
}
```
### 角色状态控制
```csharp
[GenerateEnumExtensions]
public enum PlayerState
{
Idle,
Walking,
Running,
Jumping,
Attacking
}
public class PlayerController
{
private PlayerState _state = PlayerState.Idle;
public bool CanAttack()
{
// 清晰表达可以攻击的状态
return _state.IsIn(PlayerState.Idle, PlayerState.Walking, PlayerState.Running);
}
public void HandleInput(string action)
{
if (action == "jump" && !_state.IsJumping())
{
_state = PlayerState.Jumping;
}
}
}
```
## 限制
1. **只支持枚举类型**:不能用于类或结构体
2. **不支持 Flags 枚举的特殊处理**:对于 `[Flags]` 枚举,生成的方法仍然是简单的相等比较
3. **不生成其他方法**:只生成 IsX 和 IsIn 方法
## 相关文档
- [Source Generators 概述](./index.md)
- [日志生成器](./logging-generator.md)
- [ContextAware 生成器](./context-aware-generator.md)