利用“using”的方法C# 中的指令不那么繁琐
如今,良好的编程实践往往意味着将您的内容分成许多程序集和命名空间(例如,请参阅 S#arp Architecture< /a>、MVC 等)。然而,这样做的一个副作用是,您必须将一大堆“using”指令粘贴到每个类文件中。您知道这样的事情:每个控制器类都需要“使用”模型和视图模型名称空间等。
是否有任何技术可以使这变得更容易?例如,是否可以在命名空间级别而不是文件级别声明 using
指令,以便命名空间“foo”中的每个类自动使用命名空间“bar”?或者是否有智能方法可以根据您所在的文件夹设置 Visual Studio 添加的默认“使用”?或者有其他方法可以让添加“使用”变得不那么乏味?
Good programming practice these days tends to mean splitting your stuff up into lots of assemblies and namespaces (for example, see S#arp Architecture, MVC, etc.). However a side-effect of that is that you have to stick a whole bunch of 'using ' directives into every class file. You know the sort of thing: Every controller class needs to 'use' the models and viewmodels namespaces, etc., etc.
Are there any techniques for making this easier? For example, is it possible to declare using
directives at the namespace level instead of the file level - so that every class in a namespace 'foo' automatically is using namespace 'bar'? Or are there smart ways of setting the default 'usings' that Visual Studio adds, based on the folder you are in? Or other ways of making the adding of 'usings' less tedious?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为了使添加“usings”和删除不必要的“usings”的管理不那么繁琐,我建议尝试 JetBrains 的 ReSharper< /a>.它将帮助您识别何时需要添加缺失的“using”,并将不需要的“using”语句灰显。
To make the management of adding 'usings' and removing unnecessary 'usings' less tedious, I recommend trying out JetBrains' ReSharper. It will help recognize when you need to add a missing 'using' and will grey out 'using' statements that are not needed.
正如这个问题中所讨论的,这是不可能的。
当您有很长的使用列表时,您可能也有太多的依赖性。这可能表明您需要进行一些重构。
As discussed in this question, this is not possible.
When you have long lists of usings, you possibly are also having too much dependencies. It might be an indication that you need to do some refactoring.
那么,您可以使用所有必要的用途创建自己的项目模板,并在创建新文件时使用它。
Well, you could create your own item template with all the necessary usings and use it when creating new files.
就语言而言,没有什么比你所追求的更好的了。
最好的选择是使用工具(例如 Visual Studio 中的某些工具),这些工具允许您自动为选定的“未知符号”添加用途、按字母顺序对用途进行排序,或者删除当前文件中不再需要的用途。
As far as the language goes, there is nothing like what you're after.
The best option you have is to use tools (like some of those in Visual Studio) that allow you to add usings automatically for a selected "unknown symbol", sort usings alphabetically, or strip usings that are no longer required in the current file.
在 Visual Studio 2010 中,您可以通过键入类型来减少添加;如果在 Visual Studio 中单词末尾出现红色框(当缺少 using 语句时出现),请按 Ctrl + .,同时插入符号位于类型上方宣言。这将为您提供一个快速菜单,其中包含自动添加缺少的
using
的繁琐,例如 IEnumerableusing
语句的方法,所有这些都可以通过键盘进行。或者,您可以使用常用的
using
定义一个片段,并在完成该类后使用上下文菜单 > 删除未使用的片段。组织使用。In Visual Studio 2010 you can make adding
using
s less tedious by typing the type, for example, IEnumerable<int> and if a red box appears (this appears when the using statement is missing) in Visual Studio at the end of the word pressing Ctrl + . whilst the caret is over the type declaration. This should give you a quick menu with an automatic way of adding the missingusing
statement all from the keyboard.Alternatively you could define a snippet with your common
using
s in and remove the unused ones when you have finished with the class using the context menu > organise usings.