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.
You can change the default namespace: -> Project -> XXX Properties... Instead of Find/Replace, you can also right click the namespace in code and Refactor->Rename.
The Sync Namespaces command is now available in the (right-click) menu of a project in Solution Explorer. Selecting Sync Namespaces will automatically synchronize namespaces to match your folder structure.
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.
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With