Global "use" of directives in VS2010 / C #?

I'm sure I know the answer, but I wonder if there is 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 each code file.

My interest is really related to the implementation of extension methods in the .NET Framework. The only way to use the extension method is to define the use directive for the namespace containing the extension methods. Without using the directive, I lose Intellisense's capabilities for extension methods, which means that I will not always see which methods are available.

As a framework developer, make sure that the types and methods provided within the platform are clear and accessible for developers to use, are key to me. Although the documentation and training serve their purpose, I found that most developers will click the period and scroll through the Intellisense list to find out what methods and properties are available. Even if they go to the Object Browser or look at the help documentation, they will not know about the extension method if they do not know about it. This is where Intellisense comes in.

And, although I can add the using directive to the VS template, the Delete and Sort option in VS will remove the directive that refers to extension methods if it is not used.

So, all that is said, is there a way to define a global "using" directive in VS 2010? If not, is it likely that MS will be considered for the future?

+6
c # visual-studio-2010 using-statement using-directives
source share
4 answers

You can put your extension methods in the global namespace, making sure that the contained classes are not declared in the namespace. This will make them available worldwide.

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

Of course, it is always possible for MS to consider this. Hardly, but possible.

Instead of looking for a technical solution, maybe you should take a look at the training of your fellow developers and provide an agreement on extension methods (for example, a well-known layout / namespace)?

+6
source share

To answer your actual problem, if you put your static class with extensions without a namespace, it will be available everywhere. Use with caution!

0
source share

If this is really important to you, you can buy your ReSharper company and install it on all development machines, as this will give you Intellisense for these methods, with the option of "automatically adding" the using directive.

From what I heard (I didn’t use it myself), ReSharper looks at all the possible assemblies, and I assume that you can configure it to automatically add the β€œusing” directive when creating a new class, although I'm not 100% sure about this.

0
source share

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

See Creating project templates and elements ( https://msdn.microsoft.com/en-us/library/ms247121.aspx ).

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

0
source share

All Articles