DailyRoutines.CodeAnalysis 1.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package DailyRoutines.CodeAnalysis --version 1.0.3
                    
NuGet\Install-Package DailyRoutines.CodeAnalysis -Version 1.0.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DailyRoutines.CodeAnalysis" Version="1.0.3">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DailyRoutines.CodeAnalysis" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="DailyRoutines.CodeAnalysis">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DailyRoutines.CodeAnalysis --version 1.0.3
                    
#r "nuget: DailyRoutines.CodeAnalysis, 1.0.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DailyRoutines.CodeAnalysis@1.0.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DailyRoutines.CodeAnalysis&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=DailyRoutines.CodeAnalysis&version=1.0.3
                    
Install as a Cake Tool

DailyRoutines 代码规范分析器

这个项目包含为 DailyRoutines 设计的代码规范分析器,用于确保代码风格一致性。

项目结构

DailyRoutines.CodeAnalysis/
├── Common/                     # 通用基础设施
│   ├── BaseAnalyzer.cs         # 分析器基类
│   ├── BaseCodeFixProvider.cs  # 代码修复提供者基类
│   ├── DiagnosticCategories.cs # 诊断类别常量
│   ├── DiagnosticDescriptorFactory.cs # 诊断描述符工厂
│   └── DiagnosticRules.cs      # 所有规则的描述符集合
├── Rules/                      # 规则实现
│   ├── Naming/                 # 命名规则相关
│   │   ├── DoNotUseUnderscorePrefixInNamesAnalyzer.cs
│   │   └── DoNotUseUnderscorePrefixInNamesCodeFixProvider.cs
│   ├── Usage/                  # 用法规则相关
│   │   ├── UseNintInsteadOfIntPtrAnalyzer.cs
│   │   └── UseNintInsteadOfIntPtrCodeFixProvider.cs
│   ├── Design/                 # 设计规则相关
│   │   ├── ControlStatementBodyMustBeOnNewLineAnalyzer.cs
│   │   ├── ControlStatementBodyMustBeOnNewLineCodeFixProvider.cs
│   │   ├── SingleLineControlStatementMustNotUseBlockAnalyzer.cs
│   │   └── SingleLineControlStatementMustNotUseBlockCodeFixProvider.cs
│   └── Performance/            # 性能规则相关(待添加)
└── Templates/                  # 规则模板
    ├── TemplateAnalyzer.cs     # 分析器模板
    └── TemplateCodeFixProvider.cs # 代码修复提供者模板

当前规则

规则 ID 类别 描述 严重性
DR0001 用法 使用 nint 代替 IntPtr 警告
DR0002 命名 不允许命名以下划线开头 警告
DR0004 设计 控制语句的语句体必须另起一行 警告
DR0005 设计 只有一行代码的控制语句不应使用大括号 警告

添加新规则

要添加新的规则,请按照以下步骤操作:

  1. Common/DiagnosticRules.cs 中添加新的规则描述符
  2. 选择适当的规则类别文件夹(或创建新文件夹)
  3. 复制 Templates 目录下的模板文件,根据你的规则进行修改
  4. 编译并测试新规则

添加分析器示例

  1. 复制 Templates/TemplateAnalyzer.cs 到适当的类别文件夹中,例如 Rules/Performance/AvoidUnnecessaryAllocationAnalyzer.cs
  2. 修改命名空间、类名和规则实现
  3. Common/DiagnosticRules.cs 中添加规则描述符:
/// <summary>
/// 诊断规则:避免不必要的内存分配
/// </summary>
public static readonly DiagnosticDescriptor AvoidUnnecessaryAllocation = DiagnosticDescriptorFactory.Create(
    id: "0003",
    title: "避免不必要的内存分配",
    messageFormat: "这段代码可能导致不必要的内存分配",
    category: DiagnosticCategories.Performance,
    description: "避免在性能敏感的代码路径中进行不必要的内存分配。"
);
  1. 实现新的分析器:
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class AvoidUnnecessaryAllocationAnalyzer : BaseAnalyzer
{
    public AvoidUnnecessaryAllocationAnalyzer() 
        : base(DiagnosticRules.AvoidUnnecessaryAllocation)
    {
    }

    protected override void RegisterAnalyzers(AnalysisContext context)
    {
        // 实现分析逻辑...
    }
}
  1. 如果需要,创建对应的代码修复提供程序

规则命名约定

  • 分析器类命名格式:<描述性名称>Analyzer
  • 代码修复提供者命名格式:<与分析器相同的描述性名称>CodeFixProvider
  • 规则ID格式:DR<四位数字> (例如 DR0001)

构建和测试

# 构建项目
dotnet build

# 生成NuGet包
dotnet pack -c Release

许可证

本项目采用MIT许可证。有关详细信息,请参阅 LICENSE 文件。

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.7.0 340 9/15/2025
1.6.0 252 9/15/2025
1.5.0 224 9/14/2025
1.4.0 229 9/14/2025
1.3.0 222 9/14/2025
1.2.0 202 7/30/2025
1.1.0 236 5/21/2025
1.0.9 179 5/21/2025
1.0.8 177 5/21/2025
1.0.7 174 5/21/2025
1.0.6 180 5/21/2025
1.0.5 177 5/21/2025
1.0.4 172 5/21/2025
1.0.3 179 5/21/2025
1.0.2 174 5/21/2025
1.0.1 174 5/21/2025
1.0.0 178 5/21/2025