detect-static-dependencies
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDetect Static Dependencies
检测静态依赖项
Scan a C# codebase for calls to hard-to-test static APIs and produce a ranked report showing which statics appear most frequently, which files are most affected, and which abstractions already exist in the .NET ecosystem to replace them.
扫描C#代码库中调用难以测试的静态API的情况,生成排序报告,显示哪些静态成员出现频率最高、哪些文件受影响最严重,以及.NET生态系统中已有的可替代抽象层。
When to Use
适用场景
- Auditing a project's testability before adding unit tests
- Understanding the scope of static coupling in a legacy codebase
- Prioritizing which statics to wrap first (highest-frequency wins)
- Creating a migration plan for incremental testability improvements
- 在添加单元测试前审计项目的可测试性
- 了解遗留代码库中静态耦合的范围
- 确定优先包装哪些静态成员(调用频率最高的优先)
- 创建增量提升可测试性的迁移计划
When Not to Use
不适用场景
- The user wants wrappers generated (hand off to )
generate-testability-wrappers - The user wants mechanical migration done (hand off to )
migrate-static-to-wrapper - The statics are already behind interfaces or
TimeProvider - The code is not C# / .NET
- 用户需要生成包装器(请转交至)
generate-testability-wrappers - 用户需要自动完成代码迁移(请转交至)
migrate-static-to-wrapper - 静态成员已被接口或封装
TimeProvider - 代码非C#/.NET技术栈
Inputs
输入参数
| Input | Required | Description |
|---|---|---|
| Target path | Yes | A file, directory, project (.csproj), or solution (.sln) to scan |
| Exclusion patterns | No | Glob patterns to skip (e.g., |
| Category filter | No | Limit to specific categories: |
| 输入项 | 是否必填 | 描述 |
|---|---|---|
| 目标路径 | 是 | 要扫描的文件、目录、项目(.csproj)或解决方案(.sln) |
| 排除模式 | 否 | 要跳过的Glob模式(例如 |
| 类别筛选 | 否 | 限制为特定类别: |
Workflow
工作流程
Step 1: Determine scan scope
步骤1:确定扫描范围
Resolve the target to a set of files:
.cs- If a file, scan that single file.
.cs - If a directory, scan all files recursively (excluding
.cs,obj/).bin/ - If a , find its directory and scan
.csprojfiles within..cs - If a , parse it, find all project directories, and scan
.slnfiles across all projects..cs
Always exclude , , and any user-specified exclusion patterns.
obj/bin/将目标解析为一组文件:
.cs- 如果是文件,仅扫描该单个文件。
.cs - 如果是目录,递归扫描所有文件(排除
.cs、obj/)。bin/ - 如果是,找到其所在目录并扫描其中的
.csproj文件。.cs - 如果是,解析它,找到所有项目目录,扫描所有项目中的
.sln文件。.cs
始终排除、以及用户指定的任何排除模式。
obj/bin/Step 2: Search for static dependency patterns
步骤2:搜索静态依赖模式
Scan each file for calls matching these categories:
| Category | Patterns to search for | Recommended replacement |
|---|---|---|
| Time | | |
| File System | | |
| Environment | | Custom |
| Network | | |
| Console | | |
| Process | | Custom |
扫描每个文件,查找匹配以下类别的调用:
| 类别 | 要搜索的模式 | 推荐替代方案 |
|---|---|---|
| 时间 | | |
| 文件系统 | | |
| 环境 | | 自定义 |
| 网络 | | |
| 控制台 | | 自定义 |
| 进程 | | 自定义 |
Step 3: Aggregate and rank results
步骤3:聚合并排序结果
Count each static call pattern across the entire scan scope. Produce a summary with:
- Category summary — total call sites per category (time, filesystem, env, etc.)
- Top patterns — the 10 most frequent individual patterns ranked by count
- Most affected files — files with the highest number of static dependencies
- Existing abstractions available — for each category, note the recommended .NET abstraction:
- Time → (built-in since .NET 8)
TimeProvider - File system → (NuGet package)
System.IO.Abstractions - HTTP → (built-in)
IHttpClientFactory - Environment → custom
IEnvironmentProvider - Console → custom or
IConsoleILogger - Process → custom
IProcessRunner
- Time →
统计整个扫描范围内每个静态调用模式的出现次数。生成包含以下内容的摘要:
- 类别摘要——每个类别(时间、文件系统、环境等)的总调用点数量
- 高频模式——按调用次数排序的10个最频繁的单个模式
- 受影响最严重的文件——包含最多静态依赖项的文件
- 可用的现有抽象层——针对每个类别,标注推荐的.NET抽象层:
- 时间 → (.NET 8起内置)
TimeProvider - 文件系统 → (NuGet包)
System.IO.Abstractions - HTTP → (内置)
IHttpClientFactory - 环境 → 自定义
IEnvironmentProvider - 控制台 → 自定义或
IConsoleILogger - 进程 → 自定义
IProcessRunner
- 时间 →
Step 4: Present the report
步骤4:生成报告
Format the output as a structured report:
undefined将输出格式化为结构化报告:
undefinedStatic Dependency Report
静态依赖报告
Scope: <project/solution name>
Files scanned: <count>
Total static call sites: <count>
范围: <项目/解决方案名称>
扫描文件数: <数量>
静态调用点总数: <数量>
Category Summary
类别摘要
| Category | Call Sites | Recommended Abstraction |
|---|---|---|
| Time | 42 | TimeProvider (.NET 8+) |
| File System | 31 | System.IO.Abstractions |
| Environment | 12 | IEnvironmentProvider |
| ... | ... | ... |
| 类别 | 调用点数量 | 推荐抽象层 |
|---|---|---|
| 时间 | 42 | TimeProvider (.NET 8+) |
| 文件系统 | 31 | System.IO.Abstractions |
| 环境 | 12 | IEnvironmentProvider |
| ... | ... | ... |
Top 10 Patterns
十大高频模式
| # | Pattern | Count | Files |
|---|---|---|---|
| 1 | DateTime.UtcNow | 28 | 14 |
| 2 | File.ReadAllText | 18 | 9 |
| ... |
| # | 模式 | 次数 | 涉及文件数 |
|---|---|---|---|
| 1 | DateTime.UtcNow | 28 | 14 |
| 2 | File.ReadAllText | 18 | 9 |
| ... |
Most Affected Files
受影响最严重的文件
| File | Static Calls | Categories |
|---|---|---|
| Services/OrderProcessor.cs | 12 | Time, FileSystem |
| ... |
| 文件路径 | 静态调用次数 | 涉及类别 |
|---|---|---|
| Services/OrderProcessor.cs | 12 | 时间, 文件系统 |
| ... |
Migration Priority
迁移优先级
- Time (42 sites) — Use , zero NuGet dependencies on .NET 8+
TimeProvider - File System (31 sites) — Use NuGet package
System.IO.Abstractions - ...
undefined- 时间(42个调用点)—— 使用,.NET 8+无需依赖NuGet包
TimeProvider - 文件系统(31个调用点)—— 使用NuGet包
System.IO.Abstractions - ...
undefinedStep 5: Suggest next steps
步骤5:建议后续操作
Based on the report, recommend:
- Which category to tackle first (fewest dependencies, best built-in support)
- Whether to use for custom wrapper generation
generate-testability-wrappers - Whether to use for mechanical bulk migration
migrate-static-to-wrapper
基于报告内容,推荐:
- 优先处理哪个类别(依赖最少、内置支持最好)
- 是否使用生成自定义包装器
generate-testability-wrappers - 是否使用进行批量自动迁移
migrate-static-to-wrapper
Validation
验证项
- All files in scope were scanned (check count)
.cs - Report includes category totals, top patterns, and affected files
- Each detected pattern has a recommended replacement listed
- and
obj/directories were excludedbin/ - Migration priority is ordered by impact (count × ease of replacement)
- 已扫描范围内所有文件(检查数量)
.cs - 报告包含类别总数、高频模式和受影响文件
- 每个检测到的模式都标注了推荐替代方案
- 已排除和
obj/目录bin/ - 迁移优先级按影响程度排序(调用次数 × 替换难度)
Common Pitfalls
常见陷阱
| Pitfall | Solution |
|---|---|
Scanning | Always exclude |
| Counting wrapped calls as statics | Check if the call is behind an interface or injected service before counting |
| Missing statics inside lambdas/LINQ | Search covers all code within |
Recommending | Check |
| Ignoring test projects | Only scan production code — exclude |
| 陷阱 | 解决方案 |
|---|---|
扫描 | 始终排除 |
| 将已包装的调用统计为静态依赖 | 在统计前检查调用是否已被接口或注入的服务封装 |
| 遗漏Lambda/LINQ中的静态成员 | 扫描覆盖 |
在低于.NET 8的版本中推荐 | 检查 |
| 忽略测试项目 | 仅扫描生产代码——从扫描中排除 |