Removing all unused references from a project in Visual Studio projects

前端 未结 14 2026
北荒
北荒 2020-11-29 16:41

I just wondered if it possible within various Visual Studio versions to automatically remove all references from a project that were never been used?

In your answer,

相关标签:
14条回答
  • 2020-11-29 17:20

    The following method does not depend on any 'add-on's and is not very painful.

    Step through each of your source files and

    1. Select all (Ctrl-A)
    2. Toggle outline expansion (Ctrl-M, M). This will reduce the file to two lines.
    3. Click on the namespace's '+'. This will show each of the file's classes as a single line. Scan each class's reference count, looking for unreferenced classes.
    4. Click on each of the classes' '+'. This will show each of the class functions as a single line. Scan each function's reference count, looking for unreferenced functions.

    Scanning each file looking for '0 reference' takes only a second.

    Scanning an entire project takes only a couple of minutes.

    0 讨论(0)
  • 2020-11-29 17:25

    If you have Resharper (plugin) installed, you can access a feature that allows you to analyze used references via Solution Explorer > (right click) References > Optimize References...

    http://www.jetbrains.com/resharper/webhelp/Refactorings__Remove_Unused_References.html

    This feature does not correctly handle:

    • Dependency injected assemblies
    • Dynamically loaded assemblies (Assembly.LoadFile)
    • Native code assemblies loaded through interop
    • ActiveX controls (COM interop)
    • Other creative ways of loading assemblies

    enter image description here

    0 讨论(0)
  • 2020-11-29 17:30

    All you need is stone and bare knuckle then you can do it like a caveman.

    1. Remove unused namespaces (for each class)
    2. Run Debug build
    3. Copy your executable and remaining namespace references to new location
    4. Run the executable
    5. Missing Reference DLL error will occur
    6. Copy required DLL from Debug folder
    7. Repeat 4-6
    8. Gu Gu Ga Ga?
    9. Throw your stone

    You can also rely on your build tools to let you know which reference is still required. It's the era of VS 2017, caveman still survived.

    0 讨论(0)
  • 2020-11-29 17:31

    For Visual Studio 2013/2015/2017 there is an extension that does exactly what you want: ResolveUR. What this basically does is:

    • reference is removed in the project
    • project is compiled with msbuild
    • check for build errors
    • restore removed references if there were build errors.
    0 讨论(0)
  • 2020-11-29 17:32

    With Visual Studio versions 2017 and 2015, you can do this with the Code Map feature, but this feature is only available in the Enterprise Edition, not the Community or Professional versions.

    Right-click on the project node in the solution explorer and select 'Show on Code Map.' This will display your .dll as a single node in a blank graph. Right-click on that node in the Code Map and select "Show Assemblies This References." This will add an additional node called "Externals" which can be expanded to show only the assemblies that are actually referenced.

    0 讨论(0)
  • 2020-11-29 17:32

    In Visual Studio 2013 this extension works: ResolveUR

    0 讨论(0)
提交回复
热议问题