107 lines
2.5 KiB
Markdown
107 lines
2.5 KiB
Markdown
---
|
||
applyTo: "**/*.cs"
|
||
---
|
||
|
||
# C# Base Instructions
|
||
|
||
## Language Version Constraint
|
||
This repository targets .NET Framework 4.8 and C# 7.3.
|
||
|
||
Do not use:
|
||
- record
|
||
- init property setters
|
||
- switch expressions
|
||
- using declarations
|
||
- top-level statements
|
||
- nullable reference types
|
||
- range operators (`..`)
|
||
- default interface implementations
|
||
- IAsyncEnumerable<T>
|
||
|
||
Allowed:
|
||
- async/await
|
||
- pattern matching with is / switch case patterns
|
||
- tuples
|
||
- local functions
|
||
- inline out variable declarations
|
||
- throw expressions
|
||
- ref locals and ref returns
|
||
|
||
## 中文说明
|
||
本项目目标框架为 .NET Framework 4.8,C# 版本固定为 7.3。
|
||
因此:
|
||
- 禁止使用 record、init、switch 表达式、using 声明、可空引用类型等 C# 8+ 特性
|
||
- 可以使用 async/await、元组、本地函数、模式匹配、内联 out 变量等 C# 7.3 支持的功能
|
||
|
||
## Style Rules
|
||
- Use explicit types, not `var`
|
||
- Use Allman braces
|
||
- Use 4 spaces indentation
|
||
- Use CRLF line endings
|
||
- Use block-scoped namespaces
|
||
- Place using directives outside namespaces
|
||
|
||
## 中文说明
|
||
代码风格要求:
|
||
- 显式类型,不使用 `var`
|
||
- 使用 Allman 大括号风格
|
||
- 4 空格缩进
|
||
- 使用 CRLF 换行
|
||
- 使用块作用域命名空间
|
||
- using 放在命名空间外
|
||
|
||
## Naming Rules
|
||
- Public types, methods, properties, events: PascalCase
|
||
- Private fields: _camelCase
|
||
- Constants: PascalCase
|
||
- Interfaces: I + PascalCase
|
||
- Enums: PascalCase
|
||
|
||
## 中文说明
|
||
命名规范:
|
||
- 公共类型、方法、属性、事件:PascalCase
|
||
- 私有字段:`_camelCase`
|
||
- 常量:PascalCase
|
||
- 接口:`I` + PascalCase
|
||
- 枚举:PascalCase
|
||
|
||
## Constructor Injection
|
||
Prefer constructor injection with null checks.
|
||
|
||
Example:
|
||
```csharp
|
||
public SafeAxisMotion(HardwareManager hardware, AlarmOperate alarmOperate)
|
||
{
|
||
_hardware = hardware ?? throw new ArgumentNullException(nameof(hardware));
|
||
_alarmOperate = alarmOperate ?? throw new ArgumentNullException(nameof(alarmOperate));
|
||
}
|
||
```
|
||
|
||
## 中文说明
|
||
依赖注入优先使用构造函数注入,并对参数进行 null 检查。
|
||
不要省略 null 防御。
|
||
|
||
## Async Rules
|
||
Async methods must use Async suffix
|
||
Pass CancellationToken where appropriate
|
||
Do not block async code with .Result or .Wait() unless existing architecture requires it and the reason is explicit
|
||
|
||
## 中文说明
|
||
异步规则:
|
||
- 异步方法必须加 Async 后缀
|
||
- 需要支持取消的场景必须传 CancellationToken
|
||
- 不要随意使用 .Result 或 .Wait() 阻塞异步调用,除非现有架构明确要求且理由清晰
|
||
|
||
## Code Generation Preference
|
||
Keep methods focused and readable
|
||
Prefer explicit domain terminology over generic names
|
||
Avoid unnecessary abstractions
|
||
Match the existing repository style
|
||
|
||
## 中文说明
|
||
生成代码时:
|
||
- 保持方法职责清晰
|
||
- 优先使用明确的领域术语命名
|
||
- 避免不必要的抽象
|
||
- 尽量贴合现有仓库代码风格
|