11

I am attempting to move a highly referenced class from one namespace to another. Simply moving the file into the new project which has a different root namespace results in over 1100 errors throughout my solution.

Some references to the class involve fully qualified namescape referencing and others involve the importing of the namespace.

I have tried using a refactoring tool (Refactor Pro) to rename the namespace, in the hope all references to the class would change, but this resulted in the aforementioned problem.

Anyone have ideas of how to tackle this challenge without needing to drill into every file manually and changing the fully qualified namespace or importing the new one if it doesn't exist already?

Thanks.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
Llyle
  • 5,980
  • 6
  • 39
  • 56
  • 1
    For those striking this problem. I tried Resharper, which made a real mess of things. In the end a basic 'Replace in Files' did the job. Technique for this was to replace all highly qualified references first and then lesser qualified and then manually fixing the imports. This took me 30 minutes all up. Cross cutting concerns are a pain. – Llyle Jun 18 '09 at 23:21

5 Answers5

5

Try to use Resharper. I have used it in the past for refactoring highly referenced namespaces both fully qualified and imported with no problems at all.

Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
Michael
  • 674
  • 1
  • 5
  • 13
4

Here's a Powershell script that I have used to accomplish a painful namespace rename. In my situation there were about 5000 VB.Net and C# source files making heavy use of a common library (CSLA). For reasons not worth discussing, I needed to rename the namespace Csla to CslaLegacy. With minimal effort, you should be able to adapt this script to your specific needs. The script recursively searches the source tree looking for .vb and .cs files. The $repValues hash table contains the strings that need to be replaced. A variation of this script can also be used to update project references, should your rename include an assembly name change. You can add a call to your source control tool to checkout the file before the modification. I originally did this for TFS, but found it slow to execute tf.exe. In the end it was much faster to simply checkout the entire source tree before running the script. I use PowerGUI script editor for debugging and running powershell scripts.

$root = "c:/path/to/your/source"

cd $root
$files = Get-ChildItem -Path $root -Recurse -include *.cs,*.vb 

$repValues = 
@{
'using Csla;'              = 'using CslaLegacy;';
'using Csla.Validation;'   = 'using CslaLegacy.Validation;';
'using Csla.Data;'         = 'using CslaLegacy.Data;';
'Imports Csla'             = 'Imports CslaLegacy';
'Imports Csla.Validation'  = 'Imports CslaLegacy.Validation';
}

$stmtsToReplace = @()
foreach ($key in $repValues.Keys) { $stmtsToReplace += $null }
$repValues.Keys.CopyTo($stmtsToReplace, 0)

foreach ($file in $files)
{
$path = [IO.Path]::Combine($file.DirectoryName, $file.Name)

$sel = Select-String -Pattern $stmtsToReplace -Path $path -l

if ($sel -ne $null)
{
    write "Modifying file $path" 

    (Get-Content -Encoding Ascii $path) | 
    ForEach-Object {
        $containsStmt = $false
        foreach ($key in $repValues.Keys) 
        {
            if ($_.Contains($key))
            { 
                $_.Replace($key, $repValues[$key])
                $containsStmt = $true
                break
            }
        }
        if (!$containsStmt) { $_ } 
    } |
    Set-Content -Encoding Ascii $path
}
}
Todd Stout
  • 3,687
  • 3
  • 24
  • 29
1

I don't if it's going to be helpful in your case.

  1. Modify the namespace, VS IDE will show you little red rectangular at the end of the namespace.
  2. Press Ctrl+. and select the option you like most.

If you're using tool like ReSharper, click on the namespace and press Ctrl+R. Follow instruction in Rename Namespace dialog box.

Vadim
  • 21,044
  • 18
  • 65
  • 101
  • This is exactly what I want to avoid. Going into each one of the files to correct 1100+ errors doesn't seem very productive use of developer time. – Llyle May 12 '09 at 04:03
  • You don't need to correct. VS or ReSharper will refactor it for you. You need to modify namespace only one file and namespace will be modified in all files that use the same namespace. – Vadim May 12 '09 at 04:07
  • 2
    Using Visual Studio 2008 Standard Edition to correct each of the 1100+ errors is a manual process and takes much time. ReSharper is an option, but adding another refactoring tool for namespace changing seems overkill. – Llyle May 12 '09 at 04:22
  • As Vadim said, it's not a manual process - VS does most of it for you. Could be something that's missing from Standard edition though. – UpTheCreek Jul 26 '10 at 10:35
1

It seems like you shouldn't run into too much trouble doing a global search and replace on the fully qualified name. So do a search-for-all on oldspace::myclass and replace it with newspace::myclass. Adding lines to the top of a file isn't terribly hard either, and you could probably replace

using oldspace;

with

using oldspace;
using newspace;

There are some risks with an approach like this, of course. It's quite easy to cause yourself subtle problems.

AHelps
  • 1,782
  • 11
  • 17
  • For starters it's VB, not a C-based language, but the idea of specifying both can be done with regular expression global replace. But, then I get over 500 error where the namespace is imported twice :) – Llyle May 12 '09 at 03:56
  • Ah, sorry about that, I missed the tag, and the question didn't specify. I've got to admit, when search-and-replace solutions start to take more than half an hour, I usually write myself a custom search-and-replace tool to handle my specific situation. It sounds like someone already wrote the tool you need, though, so you just have to decide whether you want to pay for it ;). – AHelps May 26 '09 at 09:35
-1

Another reason to switch to c sharp. Refactoring of source code built in for free in VS 2008.

James Westgate
  • 11,306
  • 8
  • 61
  • 68