How can I build a 'dependency tree diagram'

2019-02-06 21:01发布

问题:

I can get easily see what projects and dlls a single project references from within a Visual Studio .NET project.

Is there any application or use of reflection that can build me a full dependency tree that I can use to plot a graphical chart of dependencies?

回答1:

In addition to NDepend, you can also try this addin for Reflector for showing assembly dependency graph.



回答2:

NDepend comes with an interactive dependency graph coupled with a dependency matrix. You can download and use the free trial edition of NDepend for a while.

More on NDepend Dependency Graph

More on NDepend Dependency Matrix:

Disclaimer: I am part of the tool team



回答3:

I needed something similar, but didn't want to pay for (or install) a tool to do it. I created a quick PowerShell script that goes through the project references and spits them out in a yuml.me friendly-format instead:

Function Get-ProjectReferences ($rootFolder)
{
    $projectFiles = Get-ChildItem $rootFolder -Filter *.csproj -Recurse
    $ns = @{ defaultNamespace = "http://schemas.microsoft.com/developer/msbuild/2003" }

    $projectFiles | ForEach-Object {
        $projectFile = $_ | Select-Object -ExpandProperty FullName
        $projectName = $_ | Select-Object -ExpandProperty BaseName
        $projectXml = [xml](Get-Content $projectFile)

        $projectReferences = $projectXml | Select-Xml '//defaultNamespace:ProjectReference/defaultNamespace:Name' -Namespace $ns | Select-Object -ExpandProperty Node | Select-Object -ExpandProperty "#text"

        $projectReferences | ForEach-Object {
            "[" + $projectName + "] -> [" + $_ + "]"
        }
    }
}

Get-ProjectReferences "C:\Users\DanTup\Documents\MyProject" | Out-File "C:\Users\DanTup\Documents\MyProject\References.txt"



回答4:

You can create a dependency graph of projects and assemblies in Visual Studio 2010 Ultimate by using Architecture Explorer to browse your solution, select projects and the relationships that you want to visualize, and then create a dependency graph from your selection.

For more info, see the following topics:

How to: Generate Graph Documents from Code: http://msdn.microsoft.com/en-us/library/dd409453%28VS.100%29.aspx#SeeSpecificSource

How to: Find Code Using Architecture Explorer: http://msdn.microsoft.com/en-us/library/dd409431%28VS.100%29.aspx

RC download: http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=457bab91-5eb2-4b36-b0f4-d6f34683c62a.

Visual Studio 2010 Architectural Discovery & Modeling Tools forum: http://social.msdn.microsoft.com/Forums/en-US/vsarch/threads



回答5:

Structure101 can do that. You can browse a model by assembly and/or namespace, and clicking on any dependency at any level give you all the code-level references that cause the dependency. The .NET version is in beta, but it's been available for other languages for years, so it's very mature. Here's an example screen shot. alt text http://www.headwaysoftware.com/images/assemblies.jpg