Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# add using declaration for namespace to all classes

Tags:

c#

asp.net-mvc

Is it possible to add a using declaration to a namespace globally?

For example, I want all of my classes under the "MyApp.Models" namespace to include the namespace System.Linq.Dynamic.

In asp.net you can do this for view by adding the namespace in the web.config. Is there a way to do this for class files as well?

like image 392
Victor Avatar asked Mar 19 '12 19:03

Victor


3 Answers

No. It is not possible to add a global using declaration to a C# project. You must specify the complete set of using in each .cs file

like image 51
JaredPar Avatar answered Oct 19 '22 23:10

JaredPar


It's not possible to do what you want with obvious functionality but you could create a template that you generate your classes from and do that by namespace, the templates could include the using and this would give you the desired effect.

You could do this with basic T4 templates or something like resharper also has this functionality.

However it is a bit pointless, lots of productivity tools will bring in the using statements you need given code you write, resharper and others do this, so it is not neccessary to have a template that does this as you may end up in each namespace not using the declaration then find yourself having to delete it !

Theres not really an issue with having using statements that are unused by the way its just messy.

Ooh just in case you aren't sure what t4 templating is here is a quick link

like image 22
krystan honour Avatar answered Oct 19 '22 22:10

krystan honour


I don't believe this is possible out of the box, but you could certainly automate with a Visual Studio plugin or a plugin for a tool like Code Rush.

like image 38
Erik Dietrich Avatar answered Oct 19 '22 22:10

Erik Dietrich