开发者

Resharper- Find all unused classes

开发者 https://www.devze.com 2023-02-03 15:45 出处:网络
I know how to find unused references of a single file by right clicking on the file and selecting the \"Find Usages\" option. Is there any way I can see or get开发者_高级运维 the list of all the unuse

I know how to find unused references of a single file by right clicking on the file and selecting the "Find Usages" option. Is there any way I can see or get开发者_高级运维 the list of all the unused classes or files in my project ?


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.

Resharper- Find all unused classes


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消