DotNetAnalyzer 1.2.0
See the version list below for details.
dotnet tool install --global DotNetAnalyzer --version 1.2.0
dotnet new tool-manifest
dotnet tool install --local DotNetAnalyzer --version 1.2.0
#tool dotnet:?package=DotNetAnalyzer&version=1.2.0
nuke :add-package DotNetAnalyzer --version 1.2.0
DotNetAnalyzer
一个强大的 MCP (Model Context Protocol) 服务器工具,将 Roslyn 的代码分析能力引入 Claude Code
📖 简介
DotNetAnalyzer 是一个使用 .NET 8.0/9.0/10.0 开发的 .NET 全局工具,通过封装强大的 Roslyn (.NET Compiler Platform) API,使 Claude Code 能够深度分析和理解 C# 代码。
为什么需要 DotNetAnalyzer?
Claude Code 是一个强大的 AI 编程助手,但对于 .NET 代码的理解存在局限。DotNetAnalyzer 通过 MCP 协议桥接了这一鸿沟,提供:
- ✅ 语义级代码分析 - 不仅仅是语法高亮,而是真正的类型和符号理解
- ✅ 智能代码导航 - 跳转到定义、查找引用、理解继承层次
- ✅ 项目管理 - 解决方案分析、依赖关系、构建顺序
- ✅ 深度洞察 - 调用图分析、代码度量、复杂度评估
- ✅ 性能优化 - LRU缓存、增量分析、快速响应
作为 .NET 工具的优势
- 🚀 一键安装 - 通过
dotnet tool install快速安装 - 📦 自动更新 - 支持
dotnet tool update自动更新 - 🔧 跨平台 - 支持 Windows、macOS、Linux
- 🎯 零配置 - 开箱即用,无需手动构建
🎯 核心功能
当前版本 (v1.2.0) 提供 70 个 MCP 工具,覆盖代码分析、重构、代码质量、架构规则检查与反编译能力;所有分析能力均已达到 verified 级别。
功能概览
| 类别 | 工具数 | 说明 |
|---|---|---|
| 🔍 代码诊断 | 2 | 编译器诊断、代码度量 |
| 📁 项目管理 | 5 | 依赖分析、构建顺序、.slnx 支持 |
| 🔬 代码分析 | 6 | 语法树、覆盖率、死代码、性能与文档生成 |
| 🎯 符号查询 | 4 | 引用查找、声明定位、符号详情 |
| 🧭 导航工具 | 7 | 跳转定义、类型层次、代码度量 |
| 🔧 重构工具 | 5 | 提取方法、重命名、变量引入、重构器枚举 |
| ✨ 代码生成 | 6 | 接口实现、构造函数、格式化与 using 管理 |
| 📊 调用与比较 | 8 | 调用图、调用者/被调用者、语法树与代码差异 |
| 🧪 代码质量 | 4 | 异味检测、技术债务、综合质量报告 |
| ⚡ 代码操作 | 4 | 代码操作、重构建议、补全 |
| 🔎 高级查询 | 4 | 符号解析、定义与引用聚合、文档列表 |
| 👀 监控与可视化 | 9 | 文件监听、变更影响、缓存、依赖图与热力图 |
| 🏛️ 架构规则 | 2 | 依赖方向、层级约束、命名约定、SARIF 报告 |
| 🔬 反编译与分析 | 4 | C# 反编译、IL 分析、程序集元数据、API Surface |
支持框架: .NET 8.0 (C# 12) / .NET 9.0 (C# 13) / .NET 10.0 (C# 14)
🏗️ 架构
DotNetAnalyzer 采用分层架构设计,通过 MCP 协议连接 Claude Code 和 Roslyn 分析引擎:
核心层级:
- 用户层 - Claude Code (AI 编程助手)
- MCP 协议层 - stdio 通信,dotnet-analyzer 全局工具
- 分析引擎层 - MCP 服务器、工具注册、Roslyn 集成
- 工作区管理层 - MSBuildWorkspace、编译缓存、项目加载
- 项目层 - .NET 解决方案/项目文件
核心组件:
WorkspaceManager- LRU 缓存项目,并发加载控制CompilationCache- 编译结果缓存,自动失效ToolRegistry- 70 个 MCP 工具的注册和调用RefactoringEngine- 重构操作执行引擎PathValidator- 路径安全验证ArchitectureRuleEngine- 架构规则检查引擎DecompilationService- ILSpy 反编译服务
📄 查看详细架构图 - 包含系统架构图、组件关系图、项目结构图、MCP 工具层次图和调用流程图
🚀 快速开始
前置要求
- .NET 8.0 SDK 或更高版本
- Claude Code (支持 MCP 协议)
- 一个 .NET 解决方案或项目
安装
方式一:从 NuGet 安装(推荐)✨
DotNetAnalyzer 已发布到 NuGet.org!
# 全局安装 DotNetAnalyzer 工具
dotnet tool install --global DotNetAnalyzer
# 验证安装
dotnet-analyzer --version
# 查看工具位置
dotnet-tool list --global
NuGet 包信息:
- 📦 包名:
DotNetAnalyzer - 🏷️ 版本:
1.2.0 - 🔗 链接: https://www.nuget.org/packages/DotNetAnalyzer
- .NET 8.0 或更高版本
方式二:从源码构建
# 克隆仓库
git clone https://github.com/CartapenaBark/DotNetAnalyzer.git
cd DotNetAnalyzer
# 运行权威验证链路
bash scripts/validate-ci-cd.sh
# 从本地 NuGet 包安装
dotnet tool install --global DotNetAnalyzer --add-source ./Bin/nupkg --version 1.2.0
更新
# 更新到最新版本
dotnet tool update --global DotNetAnalyzer
卸载
# 卸载工具
dotnet tool uninstall --global DotNetAnalyzer
配置 Claude Code
在项目目录中创建 .mcp.json 文件来配置 MCP 服务器:
配置文件位置:
- 项目级配置(推荐):
.mcp.json- 放在项目根目录 - 用户级配置:
~/.claude/settings.json- 适用于所有项目
创建 .mcp.json 文件:
{
"mcpServers": {
"dotnet-analyzer": {
"command": "dotnet-analyzer",
"args": [
"mcp",
"serve"
],
"env": {
"DOTNET_ENVIRONMENT": "Production",
"DOTNET_ANALYZER_LOG_LEVEL": "Information"
}
}
}
}
或者使用项目级 settings.json:
在项目根目录创建 .claude/settings.json:
{
"enabledMcpjsonServers": ["dotnet-analyzer"]
}
然后在项目根目录创建 .mcp.json 文件(同上)。
配置优先级:
- 企业管理策略(最高)
- 命令行参数
.claude/settings.local.json(本地项目).claude/settings.json(共享项目)~/.claude/settings.json(用户级,最低)
支持的解决方案格式
DotNetAnalyzer 完全支持以下 Visual Studio 解决方案格式:
| 格式 | 扩展名 | 状态 | 说明 |
|---|---|---|---|
| 传统格式 | .sln |
✅ 完全支持 | 文本格式,Visual Studio 2010-2019 |
| 新一代格式 | .slnx |
✅ 完全支持 | XML 格式,Visual Studio 2022 17.8+ |
使用示例:
# 使用 .sln 格式
dotnet-analyzer mcp serve --solution MyProject.sln
# 使用 .slnx 格式
dotnet-analyzer mcp serve --solution MyProject.slnx
.slnx 优势:
- 🎯 人类可读的 XML 结构
- 📦 更简洁的语法
- 🚀 .NET CLI 9.0.200+ 默认格式
- ✅ 完全向后兼容 .sln
使用示例
配置完成后,你可以在 Claude Code 中自然地使用这些功能:
你: "分析这个项目的所有诊断信息"
Claude: [调用 get_diagnostics] ...
"发现了 3 个错误和 15 个警告..."
你: "这个方法的调用者有哪些?"
Claude: [调用 get_caller_info] ...
"这个方法被 5 个位置调用..."
你: "帮我提取这部分代码为一个方法"
Claude: [调用 extract_method] ...
"已成功提取为新方法 CalculateTotal..."
🛠️ 技术栈
核心技术
- .NET 8.0 - 现代化的跨平台开发框架
- .NET CLI Tools - 全局工具框架
- MCP SDK - Model Context Protocol 官方实现
- Roslyn - 微软官方 C# 编译器平台
主要依赖
<PackageReference Include="ModelContextProtocol" Version="*" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="5.*" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="5.*" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="5.*" />
<PackageReference Include="System.CommandLine" Version="2.*" />
<PackageReference Include="xUnit" Version="2.*" />
<PackageReference Include="Moq" Version="4.*" />
<PackageReference Include="FluentAssertions" Version="6.*" />
支持的解决方案格式:
- ✅ 传统
.sln格式(文本格式) - ✅ 新一代
.slnx格式(XML 格式,Visual Studio 2022+)
📦 构建和发布
本地构建
# 运行权威本地验证链路
bash scripts/validate-ci-cd.sh
# 单独查看底层命令
dotnet restore DotNetAnalyzer.slnx -p:Configuration=Release --verbosity minimal
dotnet build DotNetAnalyzer.slnx -c Release --no-restore --verbosity minimal
dotnet test DotNetAnalyzer.slnx -c Release --framework net10.0 --no-build --verbosity normal --filter "Category!=Performance"
dotnet pack src/DotNetAnalyzer.Cli/DotNetAnalyzer.Cli.csproj -c Release --no-build --output ./Bin/nupkg
GitHub Actions CI/CD
项目使用 GitHub Actions 自动化构建和发布:
- 触发条件: Push to develop branch, 创建 Release, 手动触发
- 构建流程:
- 还原依赖
- 运行测试(CI 环境跳过性能测试)
- 创建 NuGet 包
- 发布到 NuGet.org(仅 Release)
- 创建 GitHub Release
注意: 性能基准测试对运行环境敏感,在 CI 环境中会自动跳过。本地开发时可以使用
dotnet test --filter "Category=Performance"运行性能测试。
📄 查看工作流配置
版本策略
- 语义化版本: 遵循 SemVer 2.0
- 预发布版本: 使用
-beta,-rc等标识 - 自动发布: Git tag 推送时自动发布
🗺️ 开发路线图
当前公开 70 个 MCP 工具,所有分析能力均已达到 verified 级别。
| 能力域 | 状态 | 工具数 |
|---|---|---|
| 代码分析 / 导航 / 符号查询 | ✅ | 17 |
| 项目管理 / 监控 / 查询 | ✅ | 15 |
| 重构 / 代码生成 / 代码操作 | ✅ | 15 |
| 调用分析 / 比较 / 可视化 / 质量分析 | ✅ | 17 |
| 架构规则检查 | ✅ | 2 |
| 反编译与分析 | ✅ | 4 |
🤝 贡献
欢迎贡献!请查看 CONTRIBUTING.md 了解详情。
开发指南
- Fork 并克隆仓库
- 创建功能分支:
git checkout -b feature/amazing-feature - 提交变更:
git commit -m 'Add amazing feature' - 推送分支:
git push origin feature/amazing-feature - 创建 Pull Request
代码规范
⚠️ 重要: 所有贡献者必须遵守项目编码规范
📖 编码规范 (CODING_STANDARDS.md) - 必读!
- ✅ 单一真实来源(SSOT)原则
- ✅ Linux 内核编码风格
- ✅ 代码质量标准和审查检查清单
📖 开发工作流 (development-workflow.md) - 开发流程
- 📋 提交前验证清单
- 🔄 完整的开发-测试-提交流程
- 🛠️ 故障排除指南
核心要求:
- 保持单元测试覆盖率 > 80%
- 为公共 API 添加 XML 文档注释
- 编译时 0 个警告,0 个错误
- 运行
dotnet format格式化代码
本地测试工具
开发过程中可以本地安装和测试:
# 运行本地验证并生成包
bash scripts/validate-ci-cd.sh
dotnet tool install --global DotNetAnalyzer --add-source ./Bin/nupkg --version 1.2.0
# 测试工具
dotnet-analyzer --version
dotnet-analyzer mcp serve
# 完成后卸载
dotnet tool uninstall --global DotNetAnalyzer
📄 许可证
本项目采用 MIT 许可证。
📚 文档
用户指南
API 使用指南 - 完整的 MCP 工具 API 参考文档
- 当前工具分组与关键接口说明
- 参数、返回值和使用示例
- 配置选项和最佳实践
- 故障排除指南
分析能力可信度矩阵 - 稳定 / 启发式 / 实验性能力边界
- 哪些结果可以视为稳定行为
- 哪些结果会在运行时附带可信度标记
- 每项低可信能力的后续收敛路径
使用示例 - 实际使用场景和代码示例
- 基础示例(诊断检查、解决方案分析)
- 代码分析示例(结构分析、继承关系)
- 符号查询示例(查找引用、符号信息)
- 代码诊断示例(错误定位、修复建议)
- 依赖分析示例(依赖图、构建顺序)
- 综合工作流(代码审查、调试)
配置指南 - 详细的配置选项说明
- 环境变量配置
- MCP 服务器配置
- 高级配置选项
- 性能优化建议
开发者文档
项目文档
- CHANGELOG - 版本更新历史
- CONTRIBUTING.md - 贡献指南
- CODE_OF_CONDUCT.md - 行为准则
- SECURITY.md - 安全政策
社区
🙏 致谢
- Roslyn - 强大的 .NET 编译器平台
- Model Context Protocol - 连接 AI 和开发工具的标准
- Claude Code - AI 编程助手
- .NET CLI Tools - .NET 全局工具框架
📞 联系方式
- 问题反馈: GitHub Issues
- 功能建议: GitHub Discussions
- NuGet 包: DotNetAnalyzer on NuGet.org
📜 版本历史
完整更新历史请查看 CHANGELOG.md
- v1.2.0 (2026-03-28) - 架构规则检查引擎、ILSpy 反编译集成、SARIF 报告、70 个工具
- v1.1.2 (2026-03-22) - 产品可信度基线、验证链路统一与元数据修正
- v1.1.0 (2026-03-21) - 代码质量分析与产品可信度基线
- v1.0.1 (2026-03-13) - 文档优化,架构图迁移到独立文档
- v1.0.0 (2026-03-12) - 正式版,74 个 MCP 工具
- v0.8.0 - .NET 10.0 支持,框架统一
- v0.7.0 - 重构、生成和高级分析工具
- v0.6.0 - 架构优化和 CI/CD 完善
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
This package has no dependencies.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.7.0 | 74 | 4/15/2026 |
| 1.6.0 | 64 | 4/8/2026 |
| 1.5.0 | 320 | 3/29/2026 |
| 1.4.0 | 57 | 3/29/2026 |
| 1.3.0 | 64 | 3/29/2026 |
| 1.2.0 | 72 | 3/28/2026 |
| 1.1.2 | 68 | 3/26/2026 |
| 1.1.1 | 63 | 3/14/2026 |
| 1.1.0 | 64 | 3/13/2026 |
| 1.0.1 | 58 | 3/12/2026 |
| 1.0.0 | 53 | 3/12/2026 |
| 0.9.0 | 57 | 3/6/2026 |
| 0.8.0 | 65 | 2/10/2026 |
| 0.7.0 | 60 | 2/10/2026 |
| 0.6.1 | 62 | 2/9/2026 |
| 0.5.0 | 63 | 2/8/2026 |
| 0.4.0 | 68 | 2/8/2026 |
| 0.2.0 | 69 | 2/8/2026 |
v1.2.0 - Architecture Rules & Decompilation
🚀 MAJOR NEW FEATURES:
• Architecture rule checking engine (dependency direction, layer hierarchy, naming convention)
• ILSpy decompilation integration (C# decompile, IL analysis, assembly metadata, API surface)
• SARIF v2.1.0 report output for architecture checks
• Coverage report generation (HTML/Markdown)
📊 STATISTICS:
• MCP Tools: 64 → 70 (+6 new tools)
• All analysis capabilities now verified (no heuristic/experimental remaining)
• MCP SDK upgrade to 1.2.0
🎯 NEW TOOLS:
• check_architecture_rules - Check architecture rules with SARIF output
• evaluate_architecture - Evaluate architecture with custom rules
• decompile_assembly - Decompile .NET assembly to C#
• analyze_il - Analyze IL instructions of .NET assembly
• get_assembly_metadata - Read assembly metadata
• get_api_surface - Extract API surface
v1.1.2 - Product Trust Baseline
🔧 TRUST & VALIDATION:
• Unified restore/build/test/pack flow across local scripts and CI
• Added authoritative metadata source and consistency regression tests
• Fixed refactoring project/document resolution for end-to-end preview flows
• Marked heuristic and experimental analysis capabilities with explicit credibility levels
📝 DOCUMENTATION:
• Updated contributor workflow to a single recommended validation path
• Corrected CLI help, README, API guide, and package metadata drift
v1.1.0 - Code Quality Analysis
🚀 MAJOR NEW FEATURES:
• ✨ Code Smell Detection - 12 detectors (Long Method, Large Class, Circular Dependency, etc.)
• 📊 Technical Debt Quantification - Debt ratio, fix time estimation, trend analysis
• 🔍 Change Impact Analysis - Analyze impact of code changes
• 👁️ Visualization - Dependency graphs, architecture heatmaps
• 🎯 File Watching - Incremental analysis with cache support
• 📈 Quality Reports - Comprehensive quality metrics
📊 STATISTICS:
• MCP Tools: 74 → 82 (+8 new tools)
• New Detectors: 12 code smell detectors
• Lines of Code: ~15,000 → ~18,000 (+3,000)
• Backward Compatible: Yes (no breaking changes)
🎯 NEW TOOLS:
• detect_code_smells - Detect code smells with severity classification
• quantify_technical_debt - Calculate technical debt metrics
• generate_quality_report - Generate comprehensive quality report
• analyze_change_impact - Analyze impact of code changes
• start_file_watching / stop_file_watching - File monitoring
• generate_dependency_graph - Dependency graph visualization
• generate_heatmap - Architecture heatmap generation
• get_cache_statistics - Cache management
🔧 IMPROVEMENTS:
• Parallel code analysis support
• Analysis result caching (memory + persistent)
• File watching with debounce mechanism
• Analysis timeout control
• Enhanced error handling and logging
📝 DOCUMENTATION:
• Complete code quality analysis documentation
• Updated API reference
• New usage examples
v1.0.1 - Documentation Optimization
📝 IMPROVEMENTS:
• 📖 Created ARCHITECTURE.md with detailed system diagrams
• 🎯 Simplified README.md - removed complex Mermaid charts
• 📊 Moved all architecture diagrams to dedicated documentation
• 📦 Improved NuGet package README readability
📊 CHANGES:
• README.md: Reduced from 900 to ~700 lines
• Created docs/ARCHITECTURE.md with:
- System architecture diagram
- Core component relationships
- Project structure visualization
- MCP tool hierarchy
- Tool call flow sequence diagram
• Added architecture section link in README
v1.0.0 - Official Release
🎯 MAJOR IMPROVEMENTS:
• ✨ Added 4 code quality analysis tools (test coverage, dead code detection, performance analysis, documentation generation)
• 📊 Call graph visualization supports SVG, JSON, Mermaid formats
• 🌐 Localization support - error messages in Chinese and English
• 🔧 ToolBase base class reduces code duplication
• ⚡ Achieved 0 warnings, 0 errors build
• ✅ All tests passing
📊 STATISTICS:
• MCP Tools: 70 → 74 (+4)
• New Analysis Tools: 4
• Test Pass Rate: 100%
v0.8.0 - Framework Expansion & Dependency Optimization
🎯 MAJOR IMPROVEMENTS:
• ✨ Added .NET 10.0 support (C# 14)
• 📦 Unified Roslyn to version 5.0.0 across all frameworks
• 🔧 Removed conditional PackageReference compilations
• ⚡ Achieved 0 warnings, 0 errors build
• ✅ All tests passing (171 tests on net9.0/net10.0, 190 on net8.0)
📊 FRAMEWORK SUPPORT:
• .NET 8.0 (C# 12) - 190/190 tests passing ✅
• .NET 9.0 (C# 13) - 171/171 tests passing ✅
• .NET 10.0 (C# 14) - 171/171 tests passing ✅
• Dropped .NET Standard 2.0 (incompatible with modern Roslyn)
🔧 DEPENDENCIES:
• Roslyn: Unified to 5.0.0 (was: 4.14.0 on net8.0, 5.0.0 on net9.0)
• Removed BenchmarkDotNet (unused, causing NU1608 warnings)
• All NU1608 package version conflicts resolved
🐛 BUG FIXES:
• Fixed WorkspaceManager conditional compilation (NET8_0 → all frameworks)
• Fixed 23 test failures on .NET 10.0
• Removed null reference warnings
v0.7.0 - Complete Feature Implementation
🎉 Major Release - All Planned Features Completed
✨ NEW MCP TOOLS (29 total):
Phase 3 - Refactoring Tools (11):
• field_encapsulator - Encapsulate fields as properties
• interface_extractor - Extract interface from class
• signature_changer - Modify method signatures
• parameter_adder - Add parameters to methods
• temporary_inliner - Inline temporary variables
• as_remover - Remove unnecessary 'as' conversions
• unnecessary_code_remover - Remove unreachable code
• for_to_foreach_converter - Convert for to foreach
• foreach_to_for_converter - Convert foreach to for
• if_to_switch_converter - Convert if-else to switch
• for_reverser - Reverse for loop direction
Phase 4 - Code Generation (6):
• generate_interface_impl - Generate interface implementations
• generate_constructor - Generate constructors from fields
• generate_property - Generate properties
• remove_unused_usings - Remove unused using directives
• sort_usings - Sort using statements
• add_missing_imports - Add missing imports
Phase 5 - Advanced Features (8):
• get_caller_info - Analyze method callers
• get_callee_info - Analyze methods called by target
• get_call_graph - Generate complete call graphs
• compare_syntax_trees - Compare syntax trees
• get_code_diff - Generate code differences
• diagnostic_fixer - Fix compiler diagnostics
• quick_fix_provider - Provide quick fixes
• accessibility_fixer - Fix accessibility issues
📊 STATISTICS:
• MCP Tools: 41 → 70 (+29)
• Test Pass Rate: 100% (190/190)
• Compilation: Zero warnings, zero errors
• All Phases: 100% Complete
🔧 FIXES:
• Fixed 56 compilation errors
• Fixed 157 coding standard issues
• Fixed macOS CI test race conditions
技术细节:
• 5,400+ lines of new code
• 完整的中文和英文文档
• 跨平台兼容性验证
v0.6.1 - CI/CD Optimization & Multi-Platform Support
CI/CD 优化:
- ✨ 多平台构建支持 - Ubuntu、Windows、macOS 并行测试
- ✨ NuGet 包缓存 - 使用 actions/cache@v4 加速依赖还原
- 🚀 智能缓存恢复 - 基于项目文件哈希,支持部分匹配
- 📈 CI 性能测试阈值优化 - 适应 GitHub Actions 环境