What's the best way to do a bulk namespace rename on a large c# application?

后端 未结 7 1174
旧时难觅i
旧时难觅i 2020-12-15 15:41

First, a little background.

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

WidgetCompany.Depar         


        
相关标签:
7条回答
  • 2020-12-15 15:44

    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.

    0 讨论(0)
  • 2020-12-15 15:45

    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.

    0 讨论(0)
  • 2020-12-15 15:49

    Visual Studio 2019 Community Edition supports this as described here. It works for any hierarchy (with dots, root namespace changes etc) and correctly updates all dependencies.

    1. Place your cursor in the class name

    2. Press Ctrl+. to trigger the Quick Actions and Refactorings menu

    3. Select Move to namespace

    4. In the dialog box that opens, select the target namespace you'd like to move the type to

    0 讨论(0)
  • 2020-12-15 15:52

    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)

    0 讨论(0)
  • 2020-12-15 16:01

    Alternative solution if you've already partially renamed:

    • Open find and replace (shortcut ctrl + h)
    • Make sure regex is selected (icon like .*)
    • Paste the full name of your current namespace, for example YourSolution.YourProject
    • Paste this regex YourSolution.Your[partiallyRenamedNamespaceCharacters].*
    • Make sure Entire Solution is selected not Current Document
    • Run
    0 讨论(0)
  • 2020-12-15 16:05

    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.

    To completely change to the new name, you will likely need to make additional changes manually. You can find where by performing a project search (ctrl+shift+f) for other references to the name in the Project, Solution, or other supporting files from a separate text editor like VS Code. Finally, folders may need to be changed manually as well.

    0 讨论(0)
提交回复
热议问题