Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

c# - Prevent Visual Studio from adding default references and usings for new classes

Whenever I add a new class to a Visual Studio (C#) project, I get the following usings automatically:

  • using System;
  • using System.Collections.Generic;
  • using System.Linq;
  • using System.Text;

Additionally, the following DLL references are added if they weren't there already:

  • System.Core
  • System.Data
  • System.Xml

I'd like to prevent VS from doing this (except "using System" of course). Does any one know of a way to prevent this from happening?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Marc and Brian both have a good idea: create a new custom template that includes only the usings and references I want. With Export Template it's really simple to do so, and I'll be sure to do so for all sorts of specific items.

For general-purpose new classes (ie: what you get from the "Add->Class..." menu item in VS), here's what I did to achieve my goal:

  • Find the appropriate template Zip. On my system it was located at C:Program FilesMicrosoft Visual Studio 9.0Common7IDEItemTemplatesCSharpCode1033Class.zip
  • Extract the zip file. This gives two files: Class.cs and Class.vstemplate
  • Edit Class.cs to remove the undesired using statements. (I also changed the default class access modifier to "public" while I was here)
  • Edit Class.vstemplate to remove the undesired <reference> elements.
  • Rezip the files into the existing Class.zip archive
  • Replace the cached template files with the updated versions. On my system, the files were located at C:Program FilesMicrosoft Visual Studio 9.0Common7IDEItemTemplatesCacheCSharpCode1033Class.zip (a directory containing the old Class.cs and Class.vstemplate).
    • I tried simply deleting this directory, expecting VS to rebuild the cache from the "original" source. This didn't work though; I got an error message saying that it couldn't find the files in the cache directory. Replacing the cached files worked well though.
  • Restart Visual Studio

Now, whenever I add a new class, I get exactly what I want.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...