visual-studio Visual Studio:是否有“将类移动到不同的命名空间”重构?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/174193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Visual Studio: Is there a "move class to different namespace" refactoring?
提问by Seth Petry-Johnson
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 Foostatements 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)
我正在做一些架构清理,涉及将一堆类移动到不同的项目和/或命名空间中。目前我正在手动移动文件,构建,然后根据需要使用 Foo语句手动添加以解决编译错误。有人知道这样做的更聪明的方法吗?(我们是 CodeRush 和 Refactor!商店,但我很想知道 Resharper 是否对此提供支持)
采纳答案by Josh
As mentioned in the comments, this answer is now outdated. Please see the up-to-date answer below
正如评论中提到的,这个答案现在已经过时了。请参阅下面的最新答案
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!
Resharper 是我所知道的唯一具有这种能力的工具。CodeRush 和 Refactor 中还缺少许多其他功能!
回答by Darin Dimitrov
Visual Studio 2010 has the possibility to rename a namespace. Place the cursor over the namespace name and press F2. Or simply rename it in the code and press Shift+Alt+F10, Enterafter seeing the red squiggle appear.
Visual Studio 2010 可以重命名命名空间。将光标放在命名空间名称上,然后按F2。或者在看到红色波浪线出现后,只需在代码中重命名并按Shift+ Alt+ 即可。F10Enter
Reharper can also rename namespaces. Quote:
Reharper 还可以重命名命名空间。引用:
The Rename Namespacerefactoring allows users to rename a specific namespace and automatically correct all references to the namespace in the code. The following usages are renamed:
- Namespace statements
- Using directives
- Qualified names of types
在重命名命名空间重构,用户可以重命名一个特定的命名空间,并自动修正到代码中的命名空间的所有引用。以下用法被重命名:
- 命名空间语句
- 使用指令
- 类型的限定名称
回答by gschuager
Visual Studio 2019 provides at least 2 built-in options:
Visual Studio 2019 至少提供 2 个内置选项:
'Move to namespace...'refactoring can be triggered on any class, and VS will prompt for the target namespace.
'Move to namespace...'重构可以在任何类上触发,VS 会提示目标命名空间。
'Change namespace to...'refactoring is provided for when the current file namespace doesn't match with the folder structure.
当当前文件命名空间与文件夹结构不匹配时,提供“将命名空间更改为...”重构。
This can be used to move individual classes to a different namespace by:
这可用于通过以下方式将单个类移动到不同的命名空间:
- creating the desired folder structure
- moving the file
- applying the mentioned refactoring (CTRL+. with the cursor over the namespace)
- 创建所需的文件夹结构
- 移动文件
- 应用提到的重构(CTRL+. 将光标悬停在命名空间上)
These operation ensures that all references are updated accordingly.
这些操作可确保相应地更新所有引用。
回答by Gusdor
This answer applies to at least Visual Studio 2013 and 2015 with no resharper required
此答案至少适用于 Visual Studio 2013 和 2015 ,无需重新锐化
- Move class files to new folder
- Open 'Find and replace'
- Select 'Replace in Files'
- Type the originalnamespace definition in the 'Find what' field eg.
MyCorp.AppStuff.Api - Type the newnamespace definition in the 'Replace with' field eg.
MyCorp.AppStuff.Api.Extensions - Select the new folder using the 'Look in' field's browse button ..., or type the folder path
- Press the Replace Allbutton
- 将类文件移动到新文件夹
- 打开“查找和替换”
- 选择“在文件中替换”
- 在“查找内容”字段中输入原始命名空间定义,例如。
MyCorp.AppStuff.Api - 在“替换为”字段中输入新的命名空间定义,例如。
MyCorp.AppStuff.Api.Extensions - 使用“查找范围”字段的浏览按钮选择新文件夹...,或输入文件夹路径
- 按下Replace All按钮
回答by ádám Miklósi
There are partial solutions for VS 2015 & VS 2017 without Resharperusing free extensions.
有针对 VS 2015 和 VS 2017 的部分解决方案,无需使用免费扩展的Resharper。
One extension which I like today (end of 2017) is the Fix NamespaceVS Extension: https://marketplace.visualstudio.com/items?itemName=vs-publisher-599079.FixNamespace#overview
我今天喜欢的一个扩展(2017 年底)是Fix NamespaceVS 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.
它分析您的解决方案的文件夹结构并使用它提供命名空间重构。不幸的是,它并不完美:它不能很好地跟踪依赖关系,但为我解决了大部分工作。
回答by Marco Lackovic
With Resharper: CTRL+R+O
使用 Resharper:CTRL+ R+O
Then press the down arrow key twice to select Move Type To Another Namespace.
然后按两次向下箭头键选择Move Type To Another Namespace。
回答by nzduck
Since the answer above was provided (I'm guessing) this feature has been added to CodeRush. Just place the carat on the Typeto be moved and you'll see a Move Type to Namespaceoption 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.
由于提供了上面的答案(我猜),此功能已添加到 CodeRush。只需将克拉放在要移动的类型上,您就会在重构中看到将类型移动到命名空间选项!上下文菜单。这会将类型移动到新的命名空间并更新引用。不过,您可能仍希望将该文件移动到与命名空间名称匹配的解决方案文件夹中。
回答by user3285954
It's not the best outcome but can be done without plugins or tools, only with Visual Studio. Find and replace in Entire Solution, Match case, Match whole word. Find what: class name, Replace with: New.Namespace.ClassName (fully qualified class name).
这不是最好的结果,但可以在没有插件或工具的情况下完成,只有使用 Visual Studio。在整个解决方案、匹配大小写、匹配整个单词中查找和替换。查找内容:类名,替换为:New.Namespace.ClassName(完全限定的类名)。
If you have 100+ references of the moved class and other classes in old namespace what are not moved this is the only foolproof and free solution I found. The only case when it leads to errors is when you have same class name in other namespace.
如果您在旧命名空间中有 100 多个已移动类和其他未移动类的引用,那么这是我找到的唯一万无一失且免费的解决方案。导致错误的唯一情况是您在其他命名空间中具有相同的类名。
回答by Sergio
If you cannot, or do not want to use Re$harper, Notepad++is your friend:
如果您不能或不想使用 Re$harper,Notepad++是您的朋友:
- Make sure you don't have usaved changes inside Visual Studiofor the files you need to move to the new namespace
- Open allthe files that contain the namespace that needs to be changed in Notepad++
- Open Find & Replace(CTRL + H)
- Fill the Find what and Replace with fields
- Press Replace All in All Opened Documents
- Save allchanges in all documents (CTRL + SHIFT + S)
- Switch to Visual Studio and reloadall the documents (Yes to allat the prompt)
- 确保您在 Visual Studio 中没有对需要移动到新命名空间的文件进行更改
- 在 Notepad++ 中打开所有包含需要更改的命名空间的文件
- 打开Find & Replace(CTRL + H)
- 填写查找内容和替换为字段
- 按 Replace All in All Opened Documents
- 保存所有文档中的所有更改 (CTRL + SHIFT + S)
- 切换到 Visual Studio 并重新加载所有文档(Yes to all在提示符处)
DONE
完毕

