C# - 获取源代码文件正在使用或引用的类型列表

标签 c# visual-studio roslyn code-analysis code-security

我有一个很长的 .cs 文件(很多这样的文件),为了加快安全审计,我想查看这个文件所引用的类的列表。

例如:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using OrganizationName;

namespace OurCodebase
{
    public class NeedsAudit
    {
        public void DoSomething()
        {
            System.IO.File.ReadAllText( "" );
            WebRequest wr = WebRequest.Create( "" );
            OurFramework.Clients.Search( "*jason*" );
        }
    }
}

这应该会产生一个列表:

System.IO.File
System.Net.WebRequest
OrganizationName.OurFramework.Clients

有没有办法在 Visual Studio 中执行此操作,或者是否有一个程序可以解析代码并提取此列表?

最佳答案

你有很多选项我在几个项目中使用了 NDepend。 NDepend 提供的功能比您要求的要多得多,但它创建了依赖关系的可视化图表(阅读更多 here )

NDepend is a static analysis tool for .NET managed code. The tool supports a large number of code metrics, allowing to visualize dependencies using directed graphs and dependency matrix. The tool also performs code base snapshots comparisons, and validation of architectural and quality rules.

Visual Studio 2017 的 CodeMap 也可用于此类依赖项可视化任务。

CodeMap Page On MSDN :

You can visualize dependencies across your code by creating a code map. Code maps help you see how the code fits together without reading through files and lines of code.

Map dependencies with code maps

关于C# - 获取源代码文件正在使用或引用的类型列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52212919/

相关文章:

c# - 如何对不可变类构造函数进行单元测试?

visual-studio - 如何调试VS2010安装项目?

c# - csc2.exe 退出,代码为 1

java - jni4net 无法在 Java 应用程序中加载 DLL

c# - 使用 HTTP GET : ASP . NET CORE API 流式传输文件

windows - 静态链接的应用程序 - 无效或损坏的 dll

c# - 将 Roslyn 代码重构作为 NuGet 包发送

.net - 编译器错误消息: The compiler failed with error code -532462766

C# dllimport 在被 Process.Start 调用时抛出 SEHException

c++ - 使用 boost::filesystem 时出现链接器错误?