Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 348 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get list of zero-reference/unreferenced code in Visual Studio

#1
In visual studio 2013 the number of references of a special Code(method, property, field,...) is shown by __Code Lens__.
I want to get unused **(zero reference)** Code in visual studio. Is there any way to get them?

I mean below reference:
![enter image description here][1]


[1]:
Reply

#2
Here's a manual way to accomplish this that I've used for finding unused Classes that are marked public.

1. Search and Replace all "public class" with "private class" for one project in your solution. May also need to replace "public static class" and/or "public abstract class".
2. Build to find all the errors
3. For each error in the build, use your source control to restore the file for the referenced class.
4. Repeat for every error until the build succeeds.
5. Any remaining files that haven't been restored are likely candidates for removal.
6. (optional) Rename the classes in the above files and do one more build to find errors.
7. Do one last Search for the name of the class you want to remove to confirm there aren't any instances of it being used in reflection or magic strings.
8. Remove the identified unused class files.
9. Repeat for each solution project you want to clean.

Note: If you don't follow the one class per file rule, this will require a lot more work. Also, any API service endpoints you will need to verify that it is not being used by any external projects.
Reply

#3
Probably the best and easiest way to achieve what you are after is to use the build-in code analysis tool with Visual Studio to find and take you directly to dead code and unused members.

To this effect, I created a new code analysis ruleset file (Via **File->New->File**, making sure **General** in the left pane was selected and scrolling down to find **Code Analysis Rule Set**, giving it a filename, then searching for and selecting the below rules). See below for the contents of the ruleset file that you can copy, and paste into a new file with the extension .ruleset to use.

Given a ruleset file, one can right click on a project file in the **Solution Explorer** panel, and select **Properties**. In the project properties windows, click on the **Code Analysis** tab in the left panel, and then click **Open** to browse to the .ruleset file's location. If you go to the properties of a solution file (as opposed to a project file), you can set the code analysis file for each project in the solution in one place (under **Code Analysis Settings**, and using the drop-down there to select the ruleset file. NOTE: You must have previously have browsed to the ruleset file for it to show up in the drop-down in this properties window, however).

Then you simply run the code analysis on the projects/solution (Via **Analyze->Run Code Analysis On Solution** -OR- **Alt+F11**) and it will come back as warnings, any unreferenced methods or unused members it finds. It will even find methods that are referenced by a method, whom itself has no references elsewhere.

*Be careful however, as one of the ways code analysis for dead code can steer you wrong, is if the reference is 'hidden' by only ever calling the method via delegates, and of course, reflection.*

The rules to detect dead code, specifically, are:

- Private methods that are not called from any other code (CA1811)
- Unused local variables (CA1804)
- Unused private fields (CA1823)
- Unused parameters (CA1801)
- Internal classes that are not instantiated from any other code (CA1812).
- Dead code in bitwise-OR limited switch (C6259)

Below is the contents of the .ruleset file that can be had by following the steps above, for your conveinence. You can simply copy the below XML, paste it into notepad++, save somewhere with the extension **.ruleset**, browse for and use as explained 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>
Reply

#4

[To see links please register here]


> "Unfortunately, [in Visual Studio Analysis] you can only detect unused
> private members. This is because the code analyzer assumes public
> members might be used by other programs. This could be true if you’re
> publishing them as an API through a web service or releasing your code
> as a library. ... ReSharper has similar code analysis functions – with
> the advantage of checking for unused public members."
Reply

#5
In Visual Studio | Error List tab | Messages tab:
Message IDE0051 Private member 'xxxxxxxx' is unused.
Reply

#6
The best solution I can find is Resharper, you can then run the tool's analysis and look for "Type member is never used". I know this is not ideal but it's the best solution I can find.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through