简体   繁体   English

删除和排序使用在 Visual Studio 2019 中不起作用

[英]Remove and Sort Usings not working in Visual Studio 2019

On a certain solution the "Remove and Sort Usings" option does not work.在某个解决方案中,“删除和排序使用”选项不起作用。 It sorts the Usings correctly, but does not remove any unnecessary ones.它正确排序使用,但不会删除任何不必要的。 The IDE is flagging the unused ones correctly, but it can't seem to remove them. IDE 正在正确标记未使用的那些,但它似乎无法删除它们。 No errors or messages are displayed anywhere, it sorts and behaves like it has completed successfully, but the unnecessary Usings remain and are still flagged by Intellisense as such.任何地方都不会显示任何错误或消息,它的排序和行为就像它已经成功完成一样,但不必要的使用仍然存在,并且仍然被 Intellisense 标记为这样。

I have verified that in a different solution it does work, so it is not my VS2019 install.我已经验证在不同的解决方案中它确实有效,所以它不是我的 VS2019 安装。 And I don't have any plugins installed (like Resharper) that could conflict.而且我没有安装任何可能发生冲突的插件(如 Resharper)。 Have also tried clean and rebuild in case it needed an error-free compilation to work with.还尝试过清理和重建,以防它需要无错误的编译来使用。

Any ideas what could be different/special about a certain solution that would prevent the remove functionality?任何想法可能会阻止删除功能的某个解决方案有什么不同/特殊之处?

I have found another thing which causes this issue.我发现了导致此问题的另一件事。

For me, I discovered it was only happening in 1 particular project in a solution.对我来说,我发现它只发生在解决方案中的 1 个特定项目中。

After comparing the non-working csproj to a working one, I determined that the difference was the Warning Level.在将不工作的 csproj 与工作的 csproj 进行比较后,我确定差异是警告级别。

The non-working one was set to 1.非工作的设置为 1。

Resetting this to the default 4 allows the Remove and Sort Usings function to work as expected.将其重置为默认值 4 允许删除和排序使用功能按预期工作。

I found the cause, and it's an annoying one!我找到了原因,这很烦人!

There is a custom ruleset specified for static analysis of the projects, and that had both CS8019 and IDE0065 (Unnecessary using directive) unselected.为项目的静态分析指定了一个自定义规则集,并且未选择 CS8019 和 IDE0065(不必要的使用指令)。

On selecting these the remove unused usings command worked again.选择这些后,删除未使用的 using 命令再次起作用。

Thanks to other answers for suggestions.感谢其他答案的建议。

Any ideas what could be different/special about a certain solution that would prevent the remove functionality?任何想法可能会阻止删除功能的某个解决方案有什么不同/特殊之处?

This is quite a strange behavior.这是一个相当奇怪的行为。 You could try these suggestions:你可以试试这些建议:

1) close VS Instance, delete .vs hidden folder, bin , obj folder. 1)关闭 VS Instance,删除.vs隐藏文件夹、 binobj文件夹。

2) clean vs component caches under C:\\Users\\xxx\\AppData\\Local\\Microsoft\\VisualStudio\\16.0_xxxx(every this folder)\\ComponentModelCache 2)清理C:\\Users\\xxx\\AppData\\Local\\Microsoft\\VisualStudio\\16.0_xxxx(every this folder)\\ComponentModelCache下的 vs 组件缓存

3) use devenv /safemode to start VS IDE, open your project and then test again. 3)使用devenv /safemode启动 VS IDE,打开你的项目,然后再次测试。

4) If your project is an old project which means that the project structure is a bit different from VS2019, please try to create a new vs2019 project and then migrate its content into the new project. 4)如果你的项目是旧项目,这意味着项目结构与VS2019有点不同,请尝试创建一个新的vs2019项目,然后将其内容迁移到新项目中。

In addition , if these do not work, you can try this link's function to run Code Cleanup command with remove unused usings .此外,如果这些都不起作用,您可以尝试使用此链接的功能运行代码清理命令并remove unused usings

I had the same problem and figured out what was wrong.我遇到了同样的问题,并弄清楚出了什么问题。 In your Tools|Options|Text Editor|C#|Advanced look for theses settings:在您的Tools|Options|Text Editor|C#|Advanced查找这些设置:

在此处输入图片说明

HTH.哈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio 2019 中的每个项目是否有启用“淡化未使用的使用”和“删除和排序使用”的设置? - Is there a setting that enables 'Fade Unused Usings' and 'Remove and Sort Usings' per project in Visual Studio 2019? 使用不起作用的 Visual Studio 2019 自动导入 - Auto import usings not working Visual Studio 2019 我的visual studio插件如何在构建之前检测编译器错误的方式与“删除和排序使用”相同? - How can my visual studio addin detect compiler errors before building in the same manner as “Remove and Sort Usings”? 有没有排序但不删除用法的命令? - Is there a command to sort but not remove usings? 如何使用 Visual Studio 2019 和 C# 一次性添加所有缺失的使用 - How to add all missing usings at once with Visual Studio 2019 and C# 在Visual Studio 2010中显示不必要的用法 - Show unnecessary usings in Visual studio 2010 Visual Studio Watch窗口没有考虑使用 - Visual Studio Watch window not taking into account usings Visual Studio 2019 通过单击删除即时突出显示 - Visual Studio 2019 Remove instant highlighting by clicking 删除 Visual Studio 2019 上的 Source Map 部分 - Remove Source Map section on Visual Studio 2019 Visual Studio 2015 —自动解决缺少的程序集和用法 - Visual Studio 2015 — autoresolve missing assemblies and usings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM