全局“使用” VS2010 / C#中的指令? [英] Global "using" directives in VS2010/C#?

查看:82
本文介绍了全局“使用” VS2010 / C#中的指令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很确定我知道答案,但是我想知道是否可以在C#项目中定义一个全局 using指令,这样我就不必在每个代码文件的顶部重复该指令。

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.

我的兴趣真正扎根于.NET Framework中扩展方法的引入。使用扩展方法的唯一方法是为包含扩展方法的名称空间定义using指令。如果没有using指令,我将失去扩展方法的Intellisense功能,这意味着我将永远不会看到可用的方法。

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.

作为框架开发人员,请确保框架中提供的类型和方法很明确,并且可供开发人员使用对我来说很关键。尽管文档和培训可以达到目的,但我发现大多数开发人员都会按时使用并在Intellisense列表中滚动以查看可用的方法和属性。即使他们进入对象浏览器或查看参考文档,他们也不会知道扩展方法,除非他们知道它。

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.

而且,尽管我可以将using指令添加到VS使用的模板中,但VS中的删除和排序选项将删除该指令

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.

因此,说了这么多,有没有办法在VS 2010中定义全局使用指令?如果不是,那么将来MS会考虑使用它吗?

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?

推荐答案

您可以将扩展方法放在全局名称空间中,通过确保未在名称空间中声明包含的类。

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.

关于默认名称空间-除了更改VS模板外,这是不可能的。

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

当然,MS总是会考虑使用它。

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)?

这篇关于全局“使用” VS2010 / C#中的指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆