简体   繁体   English

如何更改整个项目的命名空间?

[英]how to change namespace of entire project?

I'm modifying demo application from this article: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx我正在修改本文中的演示应用程序: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

I need to update all files to use my namespace, for example now file located here:我需要更新所有文件以使用我的命名空间,例如现在位于此处的文件:

MySolution\MyApp\DemoApp\ViewModel\MainWindowViewModel.cs

is using such namespace:正在使用这样的命名空间:

namespace DemoApp.ViewModel
{
/// <summary>
/// The ViewModel for the application's main window.
/// </summary>
public class MainWindowViewModel : WorkspaceViewModel

I need to move file here (remove DemoApp folder):我需要将文件移至此处(删除 DemoApp 文件夹):

MySolution\MyApp\ViewModel\MainWindowViewModel.cs

and also to use right namespace:并使用正确的命名空间:

namespace MyApp.ViewModel
{
....

how to do that in visual studio 2010?如何在 visual studio 2010 中做到这一点?

Update ok here is possible duplicate Change Project Namespace in Visual Studio Now I know how to change the namespace of the project, but how to move files on the file system?在这里更新ok 是可能的 duplicate Change Project Namespace in Visual Studio现在我知道如何更改项目的命名空间,但是如何在文件系统上移动文件? (get rid of "DemoApp" folder) (摆脱“DemoApp”文件夹)

Go to someplace the namespace is declared in one of your files.转到命名空间在您的文件之一中声明的某个地方。 Put the cursor on the part of the namespace you want to change, and press F2 .将光标放在要更改的命名空间部分,然后按F2 This should rename the namespace in every file.这应该重命名每个文件中的命名空间。 At least, it worked in my little demo project I created to test this answer!至少,它在我为测试这个答案而创建的小演示项目中有效!

Depending on your VS version, the shortcut might also be Ctrl - R , Ctrl - R .根据您的 VS 版本,快捷方式也可能是Ctrl - RCtrl - R

I imagine a simple Replace in Files ( Ctrl + Shift + H ) will just about do the trick;我想一个简单的替换文件( Ctrl + Shift + H )就可以解决问题; simply replace namespace DemoApp with namespace MyApp .只需将namespace DemoApp替换为namespace MyApp After that, build the solution and look for compile errors for unknown identifiers.之后,构建解决方案并查找未知标识符的编译错误。 Anything that fully qualified DemoApp will need to be changed to MyApp .任何完全合格的DemoApp都需要更改为MyApp

Just right click the solution, go to properties, change "default namespace" under 'Application' section.只需右键单击解决方案,转到属性,更改“应用程序”部分下的“默认命名空间”。

在此处输入图片说明

Ctrl + Shift + H not the real solution. Ctrl + Shift + H不是真正的解决方案。

You can use Resharper to change your all namespace definitions in your solution.您可以使用 Resharper 更改解决方案中的所有命名空间定义。 This is the best way I tried before.这是我之前尝试过的最好方法。

https://www.jetbrains.com/resharper/features/code_refactoring.html https://www.jetbrains.com/resharper/features/code_refactoring.html

您可以使用CTRL + RCTRL + R或对于复杂的命名空间更改,请使用此工具https://marketplace.visualstudio.com/items?itemName=vs-publisher-599079.FixNamespace

You can use ReSharper for namespace refactoring.您可以使用 ReSharper 进行命名空间重构。 It will give 30 days free trial.它将提供 30 天的免费试用。 It will change namespace as per folder structure.它将根据文件夹结构更改命名空间。

Steps:脚步:

  1. Right click on the project/folder/files you want to refactor.右键单击要重构的项目/文件夹/文件。

  2. If you have installed ReSharper then you will get an option Refactor->Adjust Namespaces.... So click on this.如果您已经安装了 ReSharper,那么您将获得一个选项 Refactor->Adjust Namespaces.... 所以点击这个。

在此处输入图片说明

It will automatically change the name spaces of all the selected files.它将自动更改所有选定文件的名称空间。

I have gone through the folder structure with a tool called BareGrep to ensure I have got all of the namespace changes.我使用名为 BareGrep 的工具检查了文件夹结构,以确保我获得了所有命名空间更改。 Its a free tool that will allow you to search over the files in a specified file structure.它是一个免费工具,可让您搜索指定文件结构中的文件。

I tried everything but I found a solution which really works.我尝试了一切,但我找到了一个真正有效的解决方案。 It creates independed solution with a new namespace name an so on.它使用新的命名空间名称等创建独立的解决方案。

  1. In main form find namespace name -> right click -> Refactor -> Rename and select a new name.在主窗体中找到命名空间名称 -> 右键单击​​ -> 重构 -> 重命名并选择一个新名称。 Check all boxes and click OK.选中所有框并单击“确定”。

  2. In the solution explorer rename solution name to a new name.在解决方案资源管理器中,将解决方案名称重命名为新名称。

  3. In the solution explorer rename project name to a new name.在解决方案资源管理器中,将项目名称重命名为新名称。

  4. Close VS and rename folder (in total commander for example) in the solution folder to a new name.关闭 VS 并将解决方案文件夹中的文件夹(例如总指挥官)重命名为新名称。

  5. In .sln file rename old name to a new name.在 .sln 文件中,将旧名称重命名为新名称。

  6. Delete old .suo files (hidden)删除旧的 .suo 文件(隐藏)

  7. Start VS and load project启动VS并加载项目

  8. Project -> properties -> change Assembly name and default namespace to a new name.项目 -> 属性 -> 将程序集名称和默认命名空间更改为新名称。

I know its quite late but for anyone looking to do it from now on, I hope this answer proves of some help.我知道这已经很晚了,但是对于从现在开始想要这样做的任何人,我希望这个答案证明会有所帮助。 If you have CodeRush Express ( free version , and a 'must have') installed, it offers a simple way to change a project wide namespace.如果您安装了CodeRush Express免费版,并且是“必备”),它提供了一种更改项目范围命名空间的简单方法。 You just place your cursor on the namespace that you want to change and it shall display a smart tag (a little blue box) underneath namespace string.您只需将光标放在要更改的命名空间上,它就会在namespace字符串下方显示一个smart tag (一个小蓝框)。 You can either click that box or press Ctrl + keys to see the Rename option.您可以单击该框或按Ctrl +键以查看Rename选项。 Select it and then type in the new name for the project wide namespace, click Apply and select what places in your project you'd want it to change, in the new dialog and OK it.选择它,然后为项目范围的命名空间输入新名称,单击Apply并在新对话框中选择您希望它更改的项目位置,然后OK Done!完毕! :-) :-)

When I wanted to change namespace and the solution name I did as follows:当我想更改命名空间和解决方案名称时,我执行了以下操作:
1) changed the namespace by selecting it and renaming it and I did the same with solution name 1)通过选择命名空间并重命名它来更改命名空间,我对解决方案名称也做了同样的事情
2) clicked on the light bulb and renamed all the instances of old namespace 2)单击灯泡并重命名旧命名空间的所有实例
3) removed all the projects from the solution 3)从解决方案中删除所有项目
4) closed the visual studio 4)关闭视觉工作室
5) renamed all the projects in windows explorer 5)重命名Windows资源管理器中的所有项目
6) opened visual studio and added all the projects again 6)打开visual studio并再次添加所有项目
7) rename namespaces in all projects in their properties 7) 在所有项目的属性中重命名命名空间
8) removed bin folder (from all projects) 8) 删除 bin 文件夹(来自所有项目)
9) build the project again 9)再次构建项目

That worked for me without any problems and my project had as well source control.这对我来说没有任何问题,而且我的项目也有源代码控制。 All was fine after pushing those changes to the remote.将这些更改推送到遥控器后一切正常。

In asp.net is more to do, to get completely running under another namespace.在 asp.net 中要做的更多,在另一个命名空间下完全运行。

  • Copy your source folder and rename it to your new project name.复制您的源文件夹并将其重命名为您的新项目名称。
  • Open it and Replace all by Ctrl + H and be sure to include all Replace everything打开它并按 Ctrl + H 全部替换,并确保包含所有替换所有内容
  • Press F2 on your Projectname and rename it to your new project name在您的项目名称上按 F2 并将其重命名为您的新项目名称
  • go to your project properties and adjust it, coz everything has gone and you need to make a new Debug Profile Profile to Create转到您的项目属性并进行调整,因为一切都已消失,您需要创建一个新的调试配置文件来创建
  • All dependencies have now an exclamation mark - restart visual studio所有依赖项现在都有一个感叹号 - 重新启动 Visual Studio
  • Clean your solution and Run it and it should work :)清理您的解决方案并运行它,它应该可以工作:)

When renaming a project, it's a simple process重命名项目时,这是一个简单的过程

  • Rename your project重命名您的项目
  • Edit project properties to have new Default Namespace value编辑项目属性以具有新的默认命名空间值
  • Find/Replace all "namespace OLD" and "using OLD" statements in your solution查找/替换解决方案中的所有“命名空间 OLD”和“使用 OLD”语句
  • Manually edit .sln file in text editor and replace your old project name in the directory structure with your new project name.在文本编辑器中手动编辑 .sln 文件,并将目录结构中的旧项目名称替换为新项目名称。
  • Reload solution when VS prompts VS提示时重新加载解决方案

In VS 2019 you can rename your namespace using the following steps在 VS 2019 中,您可以使用以下步骤重命名命名空间

  1. Place your cursor in the namespace name.将光标放在命名空间名称中。
  2. Press Ctrl+.按 Ctrl+。 to trigger the Quick Actions and Refactorings menu.触发快速操作和重构菜单。
  3. Select Change namespace to .选择将命名空间更改为 。 For more refer to Microsoft documentation有关更多信息,请参阅Microsoft 文档

Anyone trying it out on VS Code , Use the typical Rename option, which will update all the namespace usages across, and if the project name is changed, you will have to go inside each .csproj and replace the new project name.任何在VS Code上尝试的人,使用典型的Rename选项,这将更新所有命名空间的使用,如果项目名称发生更改,您将不得不进入每个.csproj并替换新的项目名称。

Don't forget to run dotnet restore , then only it will stop showing build issues.不要忘记运行dotnet restore ,然后它才会停止显示构建问题。

Visual studio 2022 has a new feature Sync Namespace. Visual Studio 2022 有一个新功能同步命名空间。

You need to right click the project or solution in solution explorer.您需要在解决方案资源管理器中右键单击项目或解决方案。

在此处输入图像描述

If you have C# files which have been moved between folders and the namespaces are out of sync, this feature should come in handy to set the right namespace for each file based on the.如果您有 C# 个文件已在文件夹之间移动并且命名空间不同步,则此功能应该派上用场,可以根据文件为每个文件设置正确的命名空间。 format.格式。

This will especially come in handy when you are performing a migration of a legacy code base.当您执行遗留代码库的迁移时,这将特别有用。

Reference : https://nitinmanju.medium.com/de-clutter-namespaces-using-c-10-60822af79336参考资料https://nitinmanju.medium.com/de-clutter-namespaces-using-c-10-60822af79336

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM