开发者

Finding the dependencies of a class in c#

We have a c# app that uses a number of libraries (that we wrote too). The app depends on the different libraries and some of the libraries depend on some of the other libraries.

Now when it comes to testing the app we also need to make sure that the libraries are all working correctly. The problem is that the app only uses a small amount of the functionality of these libs. I can use som开发者_如何学JAVAething like NCover to find which library classes are actually used but I would also love to know how these classes (across the app and different libs) are organised - their dependency structure.

So my question: I want to be able to run my app and then get the list of classes used, organised by dependencies (ie what calls/uses what).

Does anyone know of any tool (free or not) that allow you to do that? The code is all in VS2008 organised with the different libs/app as projects.


To complete the answer of Gergely on NDepend, this tool can help does that in a smart way. Disclaimer: I am one of the developers of the tool

To achieve what you are asking for, you need to write a Code Query over LINQ (CQLinq). For example, we wrote such query, on one side we have NDepend assemblies, that are calling DevExpress assemblies. The CQLinq code query below matches DevExpress public types used by NDepend, but also iteratively, it matches internal DevExpress types used.

let devExpressTypes = Assemblies.WithNameLike("DevExpress").ChildTypes()
let ndependTypes = Assemblies.WithNameLike("NDepend").ChildTypes()
let publicDevExpressTypesUsed = devExpressTypes.UsedByAny(ndependTypes)
let devExpressTypesUsedRec = publicDevExpressTypesUsed .FillIterative(
  types=> devExpressTypes.UsedByAny(types))

from t in devExpressTypesUsedRec.DefinitionDomain
select new { t, 
   depth = devExpressTypesUsedRec[t], 
   ndependTypesUsingMe = t.TypesUsingMe.Intersect(ndependTypes) 
}

Then you can export part of the result to the NDepend dependency graph:

(Notice in the screenshot below the depth of DevExpress types. A depth of zero indicates that the public DevExpress type is directly called by a NDepend type. For such a DevExpress types, are listed the NDepend types directly using it. A depth of 1 means that the DevExpress type is used by a type directly used by a NDepend type, and so on...)

Finding the dependencies of a class in c#

...and obtain type usage graph you are asking for:

Finding the dependencies of a class in c#


I think you'll want to look at:

Reflector

from Red Gate software. It's free, and is probably the best and most well known disassembly/debugging tool for .NET. It has a plug-in architecture, too, and there is a Codeplex page, .NET Reflector AddIns that contains a number of very useful add-ins for it.

For looking at dependencies, I think the Graph plug-in should give you want you're after.

There is also a very similar (and also very good tool) on SourceForge called Refractor which will also show dependency graphs.


NDepend analyzes dlls and shows a graph with dependencies between the classes, it's a great (but not free) code analysis tool. They have a trial edition that you can use for a few months so you can give it a go.


You can try the RC release of Visual Studio 2010 Ultimate to generate dependency graphs for .NET code. You can generate a graph of all your assemblies, namespaces, classes, or some combination of these, or you can use Architecture Explorer to select specific artifacts and the relationships that you want to visualize:

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

You can use Visual Studio Ultimate to explore the relationships and organization in existing code by generating directed graph documents. These graphs represent code elements and their relationships as a set of nodes that are connected by links, or edges. You can use these graphs to help you visualize, explore, and analyze code.

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

You can select vertical sections or "slices" of code that you want to visualize by using Architecture Explorer. You can explore source code in a Visual Studio solution or compiled managed code in .dll files or .exe files. You can use Architecture Explorer to browse other domains by installing additional providers. When you find the code that you want to visualize, you can generate graphs to explore the relationships in that code.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜