开发者

Automatically Create a .cs file from a class in Visual Studio 2010

I often code in such way that I put several classes in one cs file to get my code going and then at some point, I want to move every class to a seperate .cs file within my C# project but I have to do this manually by creating a file for every class and cut and paste the class to the new file and then 开发者_开发知识库resolve the required namespace one by one for each file. No Fun!

Is there a way to just click right on the class and make visual studio to automatically move the class to a new file under the project tree? I mean, would you please provide such Macro/Snippet(or whatever it is called :) ) and instructions on how to add it to Visual Studio 2010?


If anyone is looking for a lightweight solution to this problem I found a free Move Type To File extension specifically for this purpose.

http://visualstudiogallery.msdn.microsoft.com/5746c6ea-1f92-43f0-8bab-ec6f8573cd6a


I think Resharper has the functionality you want (plus so much more).


Visual Studio 2017 now has Code Refactoring that includes Moving a class into its own file.

  • For example you can either right click on the class "Foo" and select "Quick Actions and Refactorings" (or the shortcut Ctrl +)
  • Then select "Move type into foo.cs"


  1. Ctrl M + M to Collapse the structure you are in (class/interface)
  2. Ctrl X , cut the collapsed structure
  3. Shift + Alt + C , make new CS (class file, but also you can use it for interfaces the same)
  4. Type the name of the Class/Interfaces files into the box
  5. Copy over the empty class , page Down , shift up arrow , select the class , ctrl V

6.Change the name space if you want ( optional )

This also works with previous and current version (2012) of VS.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜