views:

1267

answers:

5

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.

+1  A: 

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
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.
vanslly
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
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.
vanslly
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
+4  A: 

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.

Michael
Definitely an option, I would prefer not needing to fork out a bunch of cash for a namespace change though :)
vanslly
I know Resharper is expensive, but I can't live without it now.
Jason Heine
it is not a bunch of cash. it pays itself off in like a week.
Jon Erickson
+1  A: 

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
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 :)
vanslly
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
+3  A: 

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
A: 

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

James Westgate