GitHub 2351星:.NET 开发者的 AI 编码助手技能包

今天挖到一个宝藏仓库——dotnet/skills,专门给 AI 编码助手(比如 GitHub Copilot、Cursor 这类)准备的 .NET 和 C# 技能包。目前 2351 个 star,热度不错,而且还在持续更新。

这个仓库解决什么问题

你有没有遇到过这种情况:让 AI 写 C# 代码,结果它给你整出一些不存在的 API,或者风格完全不对路。尤其是 .NET 生态更新快,AI 模型的知识截止日期可能跟不上最新版本。

dotnet/skills 就是官方出的补救方案。它把 .NET 开发的最佳实践、常见模式、API 用法打包成 AI 能理解的技能文件,让编码助手更懂 .NET 的套路。

.NET skills pack AI assistant

核心思路:技能文件怎么工作

说白了,这玩意儿就是一份结构化的提示词集合。每个技能文件包含三个部分:

  • name:技能名称,比如 csharp-coding
  • description:告诉 AI 什么时候该用这个技能
  • instructions:具体的指令和代码示例,相当于给 AI 喂的「小抄」

AI 编码助手在遇到相关任务时,会自动加载这些技能文件,从而输出更符合 .NET 规范的代码。

完整技能模板(可直接复制)

我挑了一个最实用的 csharp-coding 技能,它专门解决 C# 代码风格和常见模式问题。下面是简化后的可复制版本:

yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
name: csharp-coding
description: >
  Basic C# coding conventions and patterns for .NET projects.
instructions: |
  ## C# Coding Conventions
  
  ### Naming
  - Use PascalCase for class names, methods, properties, and public fields
  - Use camelCase for local variables and method parameters
  - Use _camelCase for private fields
  - Avoid Hungarian notation
  
  ### Code Style
  - Use var only when type is obvious from right side
  - Prefer expression-bodied members for simple getters and methods
  - Use string interpolation instead of concatenation
  - Use async/await for I/O operations
  
  ### Common Patterns
  - Use HttpClient as singleton with IHttpClientFactory
  - Use dependency injection in constructor
  - Use records for immutable data
  - Use pattern matching over if-else chains
  
  ### Example: Good Pattern
  ```csharp
  public record User(string Name, int Age);
  
  public class UserService
  {
      private readonly IUserRepository _repository;
      
      public UserService(IUserRepository repository)
      {
          _repository = repository;
      }
      
      public async Task<User?> GetUserAsync(int id)
      {
          return await _repository.FindByIdAsync(id);
      }
  }
text
1 2 3 4 5 6 7 8

你可以直接把这个文件保存为 `csharp-coding.skill.md`,然后丢到你的项目里。支持这个格式的 AI 工具会自动识别。

## 效果演示:输入 vs 输出

**场景**:让 AI 写一个 HTTP 请求封装类

**输入(无技能)**:

写一个 C# 类来调用外部 API

text
1 2 3 4 5 6 7 8 9 10 11 12 13

**AI 输出(无技能)**:
```csharp
public class ApiClient
{
    private HttpClient client = new HttpClient();
    
    public string GetData(string url)
    {
        var response = client.GetAsync(url).Result;
        return response.Content.ReadAsStringAsync().Result;
    }
}

问题:直接实例化 HttpClient、同步阻塞、没有错误处理。

输入(使用技能)

text
1
同上,但使用 csharp-coding 技能

AI 输出(使用技能)

csharp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
public class ApiClient
{
    private readonly HttpClient _httpClient;
    
    public ApiClient(HttpClient httpClient)
    {
        _httpClient = httpClient;
    }
    
    public async Task<string?> GetDataAsync(string url)
    {
        try
        {
            var response = await _httpClient.GetAsync(url);
            response.EnsureSuccessStatusCode();
            return await response.Content.ReadAsStringAsync();
        }
        catch (HttpRequestException ex)
        {
            // Log error
            return null;
        }
    }
}

区别明显:用了依赖注入、异步、错误处理、命名规范。

C# code comparison with and without skills

变体和注意事项

  1. 不同工具适配:这个仓库的技能文件主要针对 GitHub Copilot 和 Visual Studio 的 IntelliCode。如果你用 Cursor 或其他工具,可能需要做格式转换。

  2. 版本敏感:技能文件里的示例基于 .NET 8/9,如果你还在用 .NET 6,部分 API 可能不适用。建议根据项目目标框架调整。

  3. 覆盖范围:目前仓库只覆盖了基础编码规范、ASP.NET Core 和 Entity Framework Core。如果你需要 Blazor、MAUI 等领域的技能,要么自己写,要么等社区贡献。

  4. 自定义扩展:你可以直接 fork 仓库,然后按需添加自己的技能。比如你团队有一套内部命名规范,写一个 team-conventions.skill.md 丢进去就完事。

总结

dotnet/skills 是个实用工具,尤其适合团队统一代码风格,或者让你个人的 AI 助手更懂 .NET。别指望装上就变高手,但它能减少很多重复的代码审查工作。

仓库地址:https://github.com/dotnet/skills

试试看,你可能会发现 AI 生成的代码突然「顺眼」了很多。