Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 something 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.

like image 779
David Jacka Avatar asked Oct 21 '09 08:10

David Jacka


2 Answers

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...)

Export matched types to the dependency graph

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

Types usage graph

like image 177
Patrick from NDepend team Avatar answered Oct 11 '22 05:10

Patrick from NDepend team


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.

like image 26
CraigTP Avatar answered Oct 11 '22 04:10

CraigTP