Global “using” directives in VS2010/C#?

半腔热情 提交于 2020-01-03 11:29:19

问题


I'm pretty sure I know the answer but I'm wondering if there's a way to define a global "using" directive in my C# projects so that I don't have to repeat the directive on top of every code file.

My interest is really rooted with the introduction of extension methods in the .NET Framework. The only way to use an extension method is to define a using directive for the namespace containing the extension methods. Without the using directive, I lose Intellisense capabilities for the extension methods which means I won't always see what methods are available.

As a framework developer, making sure that the types and methods that are provided in the framework are clear and available to consuming developers is key to me. While documentation and training serve their purpose, I've found that most devs will hit the period and scroll through the Intellisense list to see what methods and properties are available. Even if they go to the Object Browser or view the reference documentation, they won't know about an extension method unless they know about it. This is where Intellisense comes in.

And, while I can add the using directive to the template VS uses, the "Remove and Sort" option in VS will remove the directive referencing the extension methods if one isn't being used.

So, all that being said, is there any way to define a global "using" directive in VS 2010? If not, any chance it's being considered by MS for the future?


回答1:


You can place your extension methods in the global namespace, by making sure the containing classes are not declared within a namespace. This will make them globally available.

As for default namespaces - apart from changing the VS templates, this is not possible.

Of course, it is always possible the MS will consider it. Unlikely, but possible.

Instead of looking for a technical solution, perhaps you should look at educating your fellow developers and provide a convention regarding extension methods (for instance, a known location/namespace schema)?




回答2:


To answer your actual problem, if you put your static class with extensions in no namespace, it will be available everywhere. Use with care!




回答3:


If it's really important to you, you can have your company buy ReSharper and install it on all development machines, having this will give you Intellisense for those methods, with option to "auto add" the using directive.

From what I heard (didn't use it myself) the ReSharper scan all possible assemblies and I assume you can configure it to add "using" directive automatically when creating new class, though I'm not 100% sure in this.




回答4:


You can create .net file templates for classes that contain your using directives.

See Creating Project and Item Templates (https://msdn.microsoft.com/en-us/library/ms247121.aspx).

You can save the template as part of the project you have your extensions in; however, this is best done in a different project.



来源:https://stackoverflow.com/questions/4114096/global-using-directives-in-vs2010-c

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