Visual Studio: Is there a “move class to different

2019-01-27 12:21发布

I'm doing some architectural cleanup that involves moving a bunch of classes into different projects and/or namespaces. Currently I'm moving the files by hand, building, and then manually adding using Foo statements as needed to resolve compilation errors. Anyone know of a smarter way of doing this? (We're a CodeRush and Refactor! shop, but I'd be interested to hear if Resharper has support for this)

7条回答
forever°为你锁心
2楼-- · 2019-01-27 12:54

Since the answer above was provided (I'm guessing) this feature has been added to CodeRush. Just place the carat on the Type to be moved and you'll see a Move Type to Namespace option on the Refactor! context menu. This will move the type to the new namespace and update references. You may still want to move the file to a solution folder that matches the name of the namespace though.

查看更多
何必那么认真
3楼-- · 2019-01-27 12:57

Resharper is the only tool I am aware of what has this ability. There is also a lot of other functionality that it has that is missing in CodeRush and Refactor!

查看更多
劳资没心,怎么记你
4楼-- · 2019-01-27 13:09

This answer applies to at least Visual Studio 2013 and 2015 with no resharper required

  1. Move class files to new folder
  2. Open 'Find and replace'
  3. Select 'Replace in Files'
  4. Type the original namespace definition in the 'Find what' field eg. MyCorp.AppStuff.Api
  5. Type the new namespace definition in the 'Replace with' field eg. MyCorp.AppStuff.Api.Extensions
  6. Select the new folder using the 'Look in' field's browse button ..., or type the folder path
  7. Press the Replace All button
查看更多
走好不送
5楼-- · 2019-01-27 13:10

With Resharper: CTRL+R+O

Then press the down arrow key twice to select Move Type To Another Namespace.

查看更多
Anthone
6楼-- · 2019-01-27 13:10

If you cannot, or do not want to use Re$harper, Notepad++ is your friend:

  1. Make sure you don't have usaved changes inside Visual Studio for the files you need to move to the new namespace
  2. Open all the files that contain the namespace that needs to be changed in Notepad++
  3. Open Find & Replace (CTRL + H)
  4. Fill the Find what and Replace with fields
  5. Press Replace All in All Opened Documents
  6. Save all changes in all documents (CTRL + SHIFT + S)
  7. Switch to Visual Studio and reload all the documents (Yes to all at the prompt)

DONE

查看更多
你好瞎i
7楼-- · 2019-01-27 13:12

There are partial solutions for VS 2015 & VS 2017 without Resharper using free extensions.

One extension which I like today (end of 2017) is the Fix Namespace VS Extension: https://marketplace.visualstudio.com/items?itemName=vs-publisher-599079.FixNamespace#overview

It analyses the folder structure of your solution and offers namespace refactoring using that. Unfortunately it isn't perfect: It doesn't track dependencies that well, but solved the lion's share of the work for me.

查看更多
登录 后发表回答