跳到正文
Skillpedia
通用编程40k · #7177

dotnet-best-practices

它能做什么

这个 AI 代理可以帮助你确保 .NET/C# 代码遵循推荐的最佳实践。它会检查以下方面:文档注释、代码结构、设计模式的使用、依赖注入、资源管理、async/await 的用法、测试、配置、AI 集成、错误处理、安全性以及整体代码质量。

适用场景

  • ✓当你编写新的 C# 代码时。
  • ✓当你审查已有代码时。
  • ✓当你想优化性能或安全性时。

技能链接

https://github.com/github/awesome-copilot/blob/main/skills/dotnet-best-practices/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/github/awesome-copilot/blob/main/skills/dotnet-best-practices/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“检查一下这段 C# 代码是否遵循了最佳实践。”

每周 5 个新技能,1 分钟读完。

工作原理

代理会分析你的代码中是否存在特定模式,例如文档是否完善、设计模式是否运用得当、错误处理是否正确。它会将你的代码与一套规则进行比对,并提出改进建议。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

.NET/C# Best Practices

Your task is to ensure .NET/C# code in ${selection} meets the best practices specific to this solution/project. This includes:

Documentation & Structure

  • Create comprehensive XML documentation comments for all public classes, interfaces, methods, and properties
  • Include parameter descriptions and return value descriptions in XML comments
  • Follow the established namespace structure: {Core|Console|App|Service}.{Feature}

Design Patterns & Architecture

  • Use primary constructor syntax for dependency injection (e.g., public class MyClass(IDependency dependency))
  • Implement the Command Handler pattern with generic base classes (e.g., CommandHandler<TOptions>)
  • Use interface segregation with clear naming conventions (prefix interfaces with 'I')
  • Follow the Factory pattern for complex object creation.

Dependency Injection & Services

  • Use constructor dependency injection with null checks via ArgumentNullException
  • Register services with appropriate lifetimes (Singleton, Scoped, Transient)
  • Use Microsoft.Extensions.DependencyInjection patterns
  • Implement service interfaces for testability

Resource Management & Localization

  • Use ResourceManager for localized messages and error strings
  • Separate LogMessages and ErrorMessages resource files
  • Access resources via _resourceManager.GetString("MessageKey")

Async/Await Patterns

  • Use async/await for all I/O operations and long-running tasks
  • Return Task or Task<T> from async methods
  • Use ConfigureAwait(false) where appropriate
  • Handle async exceptions properly

Testing Standards

  • Use MSTest framework with FluentAssertions for assertions
  • Follow AAA pattern (Arrange, Act, Assert)
  • Use Moq for mocking dependencies
  • Test both success and failure scenarios
  • Include null parameter validation tests

Configuration & Settings

  • Use strongly-typed configuration classes with data annotations
  • Implement validation attributes (Required, NotEmptyOrWhitespace)
  • Use IConfiguration binding for settings
  • Support appsettings.json configuration files

Semantic Kernel & AI Integration

  • Use Microsoft.SemanticKernel for AI operations
  • Implement proper kernel configuration and service registration
  • Handle AI model settings (ChatCompletion, Embedding, etc.)
  • Use structured output patterns for reliable AI responses

Error Handling & Logging

  • Use structured logging with Microsoft.Extensions.Logging
  • Include scoped logging with meaningful context
  • Throw specific exceptions with descriptive messages
  • Use try-catch blocks for expected failure scenarios

Performance & Security

  • Use C# 12+ features and .NET 8 optimizations where applicable
  • Implement proper input validation and sanitization
  • Use parameterized queries for database operations
  • Follow secure coding practices for AI/ML operations

Code Quality

  • Ensure SOLID principles compliance
  • Avoid code duplication through base classes and utilities
  • Use meaningful names that reflect domain concepts
  • Keep methods focused and cohesive
  • Implement proper disposal patterns for resources
常见问题

什么是“最佳实践”?

最佳实践是编写代码最高效、最安全的方式,它能让代码更易维护,并避免常见错误。

使用这个代理需要懂很多技术术语吗?

不需要。即使你不是经验丰富的程序员,这个代理也能提供帮助。它的重点是应用那些能提升代码质量的规则。

代理能自动修复我的代码吗?

这个代理主要负责发现问题并提出改进建议。自动修正可能需要依赖其他工具,或者需要你自己动手处理。

详情

作者的原始描述

Ensure .NET/C# code meets best practices for the solution/project.

repo: github/awesome-copilot

热度

GitHub 星标
40k
排名
#7177
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能