Are there any good tools or tricks for determining if there are any referenced but unused dependencies (such as dlls) in a project?
My specific case is C# .net3.5.
Are there any good tools or tricks for determining if there are any referenced but unused dependencies (such as dlls) in a project?
My specific case is C# .net3.5.
Well a duct-tape solution would be to remove the reference and attempt to compile.
Isn't this functionality built right into Visual Studio? Project Properties -> References -> Find Unused?
I would recommend ReSharper from JetBrains. It will first help you to see what using statements are not necessary in your code and you can also find out what references are not used in your project (Find Usage and if the result is none, then you know the reference is not necessary). I have to admit that this part is manual, but of the other code cleaning actions can be automated using code formatting.
Yes you can use NDepend for this because the tool comes with the Afferent Coupling metric for Assemblies, Namespaces, Types, Methods and Fields. For example, the Afferent Coupling for a particular method is the number of methods that depends directly on it.
Thus code elements with Afferent Coupling set to 0 are likely not used. I say likely because it is mathematically not possible to statically know which code element is used or not at runtime. But you can use the flexibility of Code Query Language like for example asking for method with no Afferent Coupling and that are not the Main() method (Entry Point), a static ctor or a finalizer.
// <Name>Potentially unused methods</Name>
WARN IF Count > 0 IN SELECT METHODS WHERE
MethodCa == 0 AND // Afferent Coupling is 0
!IsEntryPoint AND // Main() method is not used by-design.
!IsClassConstructor AND // The IL code never explicitely calls class constructors.<br>
!IsFinalizer // The IL code never explicitely calls finalizers.
ReSharper helped! Context menu of the reference -> "Find dependent Code". The nice thing is that Resharper also shows you the places where you use the specific reference (in a nice treeview). Anyways, it would be nice to have a cleanup function as there is for VB users built in visual studio.
Removing unused references is a feature Visual Studio 2008 already supports. Unfortunately, only for VB .NET projects.
I have opened a suggestion on Microsoft Connect to get this feature for C# projects too:
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=510326
If you like this feature as well then you might vote my suggestion.
It's lame that VS doesn't support this for C#, and that seems to be the case in 2010 too. Would have voted up jbe's suggestion, but the link is broken. Also, I dislike ReSharper so that isn't an option for me.
Anyway, my method has been to:
This works because a reference in VS is just a hint to the compiler that you might need that DLL. If you don't actually use any types in the referenced assembly, the compiler won't include the reference in the assembly's metadata. Thus, Reflector will report on those assemblies that you actually refer to in IL.
Of course, you might be reflectively using assemblies, or you might have added the reference in VS just to get the DLL copied to the same directory as the referencing assembly. Or you may have added the reference because the compiler told you it needed it because you use a dependent assembly. Keep these issues in mind, don't blindly delete references, and be sure to regularly rebuild each project as you go.
HTH,
Kent