C# - Get List of types that a source code file is using or refering to

403 Views Asked by At

I have a long .cs file (many such files) and to speed up the security audit I'd like to see a list of classes that this file is refering to.

For example:

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*" );
        }
    }
}

This should result in a list:

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

Is there a way to do this in Visual Studio, or perhaps a program that would parse the code and extract this list?

2

There are 2 best solutions below

0
Ali M On

You have many options I used NDepend in several projects. NDepend offers a lot more than what you asked but it creates a visual graph of dependencies (read more 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.

CodeMap for visual studio 2017 is also available for this kind of dependency visualization tasks.

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

0
Tushar On

Apart from CodeMap and NDepend, you may try my tool Designite especially if you want to see the dependencies in a matrix format. The DSM (Dependency Structure Matrix) provided by Designite allows you to see dependencies interactively at different granularities (Types, Namespaces, and Projects).

DSM by Designite