What's the best way to do a bulk namespace ren

2019-01-23 10:45发布

First, a little background.

Currently namespaces and assemblies in our codebase (~60 assemblies, thousands of classes) looks like

WidgetCompany.Department.Something

We have now been spun off such that we are selling the software that drives a WidgetCompany, so we 'd like to rename the namespaces & assemblies

NewCompany.Something

Under normal circumstances I'd probably just stick with the old namespace, but the problem is our customers don't want to see the name of one of their competitors anywhere in the application. In the off chance that they see a stack trace, assembly properties etc, it shouldn't show up. It's not meant to hide our associates or do anything sinister, we just need to make sure that people know we are a separate entity, separate management, and they don't need to worry about data being shared etc.

Now the question. What is the best way to perform this type of all encompassing rename? The following would need to change:

  • Namespace for (almost) every class in the application
  • Every using statement in the application which references the old names
  • Folder structure for each project
  • References between projects which rely on changed folder structure
  • .Sln files which reference the changed folder structure
  • Any references to those classes which are fully qualified (should be few and far between)
  • Any references to those classes in xml config files (config sections etc)
  • AssemblyInfo.cs files for every assembly
  • AssemblyName in every .csproj file

Am I stuck with the find-replace-pray strategy or is there something better?

5条回答
何必那么认真
2楼-- · 2019-01-23 11:13

Firstly I would try Refactor->Rename option. But as mentioned in comment for another answers it doesn't work that good (Or I haven't found how to make it working). So I prefer using following scenario, especially if you want to add some addiotional namespace.

1) Rename your root namespace (WidgetCompany) to something like NAMESPACE_TO_BE_REPLACED using Refactor-Rename
2) Replace NAMESPACE_TO_BE_REPLACED with your final namespace (NewCompany.WidgetSoftware) using find-n-replace dialog

And do not forget to rename projects, default namespaces, etc.

查看更多
闹够了就滚
3楼-- · 2019-01-23 11:25
  • Open a random class in [WidgetCompany.Department.Something]
  • Edit->Find and Replace->Replace in Files
  • Type "WidgetCompany.Department.Something" in FindWhat area
  • Type "NewCompany.Something" in Replace with area
  • Select Current Project in Look in area.
  • Replace All
查看更多
地球回转人心会变
4楼-- · 2019-01-23 11:30

ReSharper. Get version 5.1 from JetBrains for free for 30 days (more than enough time to do your renaming). Put the cursor on the namespace you want to change and hit Ctrl-R twice (ReSharper Rename). It'll work with a namespace any number of levels deep, and converts any usage of that namespace to the new one. However, you will have to change every unique namespace in your solution (unless you just go with Find/Replace)

查看更多
再贱就再见
5楼-- · 2019-01-23 11:38

If you have ReSharper:

  1. Right click project, Properties. Change Default namespace to desired namespace.

  2. Right click project, Refactor -> Adjust Namespace to update all the namespaces to use the default namespace

Then just let it do its magic.

查看更多
做自己的国王
6楼-- · 2019-01-23 11:39

Right click on your current namespace and select Refactor -> Rename and change the name in the pop up that comes up after a while. Enter your new name and click ok.

If you have multiple depths to your namespace, then Visual Studio won't let you type a dot. However, if you copy and paste a dot, despite a warning, it will do the business.

查看更多
登录 后发表回答