Get List of Zero Reference Codes in Visual Studio

前端 未结 4 2012
忘掉有多难
忘掉有多难 2020-11-27 10:08

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 refe

4条回答
  •  星月不相逢
    2020-11-27 10:24

    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.

提交回复
热议问题