Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resharper- Find all unused classes

Tags:

c#

resharper

People also ask

How to Find unused classes in visual studio code?

To find unused members with a Code Analysis Ruleset, from the Visual Studio menu select File -> New -> File… -> General -> Code Analysis Rule Set. Uncheck all the rules.

How to Find unused code in c#?

Right click on your solution and selection "Find Code Issues". One of the results is "Unused Symbols". This will show you classes, methods, etc., that aren't used.


First enable "Analyze Errors In Solution" (right-click on the Resharper icon in the status bar).

Then right-click the solution node and select "Find Code issues". In the "Inspection results" tool window you can group by "Issue type" and look for "Type or type member is never used" to get all unused classes (and many more unused symbols).

A second option (after enabling "Analyze Errors In Solution") is to go to any unused class, hit Alt+Enter, select "Options for 'Type or type member ...' inspection" | Find all code issues of this type | Entire solution.

Inspection results window sample


Solution by @ulrichc sounds perfect and works fine for small to middleweight projects in which you are not using any Dependency Injection framework such as Castle or Ninject but what if you are using DI container [Castle for instance] and you have something like the following :

public class IoC
{
    private WindsorContainer _container;

    private IoC()
    {
         _container = new WindsorContainer();
    }

    public static void RegisterFromAssembly(Assembly assembly, string classEndsWith, LifeTime lifeTime)
    {
        var lifestyle = ConvertLifeStyleType(lifeTime);

        _container.Register(AllTypes.FromAssembly(assembly)
                  .Where(type => type.Name.EndsWith(classEndsWith))
                  .WithService.AllInterfaces()
                  .Configure(c => c.LifeStyle.Is(lifestyle))
                  .WithService.FirstInterface());
    }
}

As you can see RegisterFromAssembly goes through all the types inside the assembly and blindly [based on the methods parameter] adds them to the container at Run-time.

You will need something like Agent Mulder plugin which provides navigation for types registered or resolved inside your containers. This again might visually [design time possibly] work but you would not really be sure unless every time you remove the unused class you run all the tests inside your application [every possible layer] to be 80% sure you are safe. Moral of the story : a class might sound unused to Resharper but it might be resurrected when you use Dependency Injection.