Probably the best way (read: the easiest) to achieve what you need is to use the built-in code analysis tool with Visual Studio to find and take you directly to the dead code and unused members.
To do this, I simply created a new code set rule file (Via File-> New-> File , making sure General is selected in the left pane and scroll down to find the Code analysis rule set by specifying the file name, then searching and selecting the below the rule). Below is the contents of the rule set file, which you can simply copy and paste into a new file with the .ruleset extension for use.
Based on the rule set file, right-click the project file in the Solution Explorer pane and select Properties . In the project properties window, click the Code Analysis tab in the left pane, and then click Open to navigate to the location of the .ruleset file. If you go to the properties of the solution file (as opposed to the project file), you can install the code analysis file for each project in the solution in one place (in the Code analysis settings section) and use the drop-down list to select the rule set file. NOTE: You should have previewed the rule set file so that it appears in the drop-down list in this properties window, however).
Then you simply start the code analysis on the projects / solution (through Analysis-> Perform code analysis on the solution -OR- Alt + F11 ), and it will come back as warnings, any undescribed methods or unused members that it finds. He will even find methods that are referenced by a method that itself does not have references elsewhere.
However, be careful, as one of the ways to analyze code for dead code may affect you incorrectly, if the link is "hidden", only ever calling the method through delegates and, of course, reflection.
The rules for detecting dead code, in particular, are as follows:
- Private methods that are not called from any other code (CA1811)
- Unused Local Variables (CA1804)
- Unused Private Fields (CA1823)
- Unused Parameters (CA1801)
- Inner classes that are not created from any other code (CA1812).
- Dead Code on Bit or Restricted Switch (C6259)
The following is the contents of the .ruleset file that you can follow by following the steps above for your compliance. You can simply copy the XML below, paste it into Notepad ++, save it somewhere with the .ruleset extension, view and use it as described above:
<?xml version="1.0" encoding="utf-8"?> <RuleSet Name="Dead Code Rules" Description=" " ToolsVersion="12.0"> <Rules AnalyzerId="Microsoft.Analyzers.ManagedCodeAnalysis" RuleNamespace="Microsoft.Rules.Managed"> <Rule Id="CA1801" Action="Warning" /> <Rule Id="CA1804" Action="Warning" /> <Rule Id="CA1811" Action="Warning" /> <Rule Id="CA1812" Action="Warning" /> <Rule Id="CA1823" Action="Warning" /> </Rules> <Rules AnalyzerId="Microsoft.Analyzers.NativeCodeAnalysis" RuleNamespace="Microsoft.Rules.Native"> <Rule Id="C6259" Action="Warning" /> </Rules> </RuleSet>
Hope this helps you and don't forget to choose the best answer.