Resharper- Find all unused classes

大憨熊 提交于 2019-11-28 04:01:45

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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!