共用方式為


Item templates - adding references by default

A recent bug entered via MSDN Product Feedback Center questions the current behavior of add new item with respect to the class template. In particular, if a class is added to a project then references to System.dll, System.Data.dll, and System.XML.dll are added automatically. This can certainly be a frustrating experience if the project doesn’t require those references and the developer is trying to keep the set of references pristine.

There are a couple of reasons for the current design. First, there is a bit of historical reasoning. Both VS 2002 and VS 2003 supported this behavior. When we authored the item templates for VS 2005 we decided that we would only make functionality changes when we had received substantial feedback that the model or content was poor. Since we didn’t have that feedback about the reference addition, we didn’t make the change. The second reason has to do with a combination of the popularity of those particular references plus the behavior of a feature that we added to C# in VS 2005 called ‘add using’. Add using will proffer a smart tag on a type which allows a using directive to be automatically added to the file/namespace that you’re working in; however, it requires that the assembly which contains that type already be referenced by the project. Given that these two assemblies contain types which are extremely common in a number of projects, we decided that continuing to add them automatically would enhance the experience. It’s also important to note that the C# compiler will not emit references to assemblies when the code that is being compiled does not contain any of the types defined in those assemblies (so there isn’t really an additional cost outside of readability for having those particular assemblies referenced).

Given that we still haven’t received a lot of feedback outside of the bug mentioned above, we’re going to keep the behavior the same for the moment (obviously if we receive more feedback we’ll reconsider). However, the good news is that it’s possible to customize this behavior fairly easily. The C# item templates that are shipped with Visual Studio are installed to %Program Files%\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\CSharp\1033. The Add | Class… template is in the Class.zip file. There are two files in the zip; the Class.cs file is the general shape of the generated file. It has a few replacement strings inside of it that are determined based on the “Root namespace” of the project, or the filename the is chosen when the class is added. The Class.vstemplate file is a description of the contents of the template, and allows the icon, description, etc. of the class template to be changed (these are displayed in the Add New Item… dialog). The .vstemplate file also contains the list of references to use when a new class is added to the project. In order to change this behavior, simply change:

<Reference>

<Assembly>System</Assembly>

</Reference>

<Reference>

<Assembly>System.Data</Assembly>

</Reference>

<Reference>

<Assembly>System.Xml</Assembly>

</Reference>

to:

<Reference>

<Assembly>System</Assembly>

</Reference>

<!--<Reference>

<Assembly>System.Data</Assembly>

</Reference>

<Reference>

<Assembly>System.Xml</Assembly>

</Reference>-->

Then re-zip the contents and close all instances of Visual Studio. Then run devenv /setup (from the %Program Files%\Microsoft Visual Studio 8\Common7\IDE directory). I didn't try it, but it should be possible to run devenv /InstallVSTemplates instead which sould execute faster. This will recreate the cache that VS uses to store the unzipped versions of the item templates. When this is done, start VS. The Add | Class… item template will no longer add a reference to System.Data.dll or System.Xml.dll to your project.

Comments

  • Anonymous
    April 09, 2007
    The other day I was working on a demonstrator application for the Object Builder framework, and adding
  • Anonymous
    May 04, 2007
    Finally! I don't have to keep deleting these darned references every time I add a class!
  • Anonymous
    June 12, 2007
    thanks!  this is especially useful when compiling silverlight projects in vs2005
  • Anonymous
    October 09, 2007
    This "feature" of VS deciding for me which assemblies to add into my project is annoying and paternalistic. It's either insulting ("the user isn't smart enough to add what he/she needs") or invasive ("we have decided that all .NET projects should be built like this"). Why don't you trust the developer and empower him/her to craft the system?
  • Anonymous
    June 28, 2008
    I don't see why System.Data & System.Xml are that useful. I know it has a lot of stuff in there, but we aren't all database programmers. Instead, because I'm perfectionist, I have to delete those reference all the time. I'm so glad there's actually a way to fix that.
  • Anonymous
    February 19, 2009
    Legend, this works a treat in VS2008 too, was thoroughly fed up of removing the using System.Linq every time I created a class! Thanks.