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.
我试图将一个高度引用的类从一个名称空间移动到另一个名称空间。简单地将文件移动到具有不同根命名空间的新项目中会导致整个解决方案中出现1100多个错误。
Some references to the class involve fully qualified namescape referencing and others involve the importing of the namespace.
对类的一些引用涉及完全限定的namescape引用,其他引用涉及命名空间的导入。
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.
我尝试使用重构工具(Refactor Pro)来重命名命名空间,希望对类的所有引用都会改变,但这导致了上述问题。
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.
5 个解决方案
#1
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.
尝试使用Resharper。我过去曾使用它来重构高度引用的命名空间,无论是完全限定还是导入都没有问题。
#2
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.
这是我用来完成痛苦的命名空间重命名的Powershell脚本。在我的情况下,大约有5000个VB.Net和C#源文件大量使用公共库(CSLA)。由于不值得讨论的原因,我需要将命名空间Csla重命名为CslaLegacy。只需很少的努力,您就应该能够根据您的特定需求调整此脚本。该脚本以递归方式搜索源树,查找.vb和.cs文件。 $ repValues哈希表包含需要替换的字符串。如果重命名包含程序集名称更改,则此脚本的变体也可用于更新项目引用。您可以在修改之前添加对源代码管理工具的调用以签出文件。我最初是为TFS做的,但发现执行tf.exe很慢。最后,在运行脚本之前简单地检查整个源树要快得多。我使用PowerGUI脚本编辑器来调试和运行PowerShell脚本。
$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
}
}
#3
I don't if it's going to be helpful in your case.
如果它对你的情况有帮助我不会。
- Modify the namespace, VS IDE will show you little red rectangular at the end of the namespace.
- Press Ctrl+. and select the option you like most.
修改命名空间,VS IDE将在命名空间的末尾显示小红色矩形。
按Ctrl +。并选择您最喜欢的选项。
If you're using tool like ReSharper, click on the namespace and press Ctrl+R. Follow instruction in Rename Namespace dialog box.
如果您使用的是ReSharper之类的工具,请单击命名空间并按Ctrl + R.按照“重命名命名空间”对话框中的说明操作
#4
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
看起来你不应该在全局搜索和替换完全限定名称时遇到太多麻烦。所以在oldspace :: myclass上进行搜索并将其替换为newspace :: myclass。在文件顶部添加行也不是很难,你可以替换
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.
当然,这样的方法存在一些风险。很容易引起自己微妙的问题。
#5
Another reason to switch to c sharp. Refactoring of source code built in for free in VS 2008.
切换到c锐利的另一个原因。重构VS 2008中免费内置的源代码。
#1
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.
尝试使用Resharper。我过去曾使用它来重构高度引用的命名空间,无论是完全限定还是导入都没有问题。
#2
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.
这是我用来完成痛苦的命名空间重命名的Powershell脚本。在我的情况下,大约有5000个VB.Net和C#源文件大量使用公共库(CSLA)。由于不值得讨论的原因,我需要将命名空间Csla重命名为CslaLegacy。只需很少的努力,您就应该能够根据您的特定需求调整此脚本。该脚本以递归方式搜索源树,查找.vb和.cs文件。 $ repValues哈希表包含需要替换的字符串。如果重命名包含程序集名称更改,则此脚本的变体也可用于更新项目引用。您可以在修改之前添加对源代码管理工具的调用以签出文件。我最初是为TFS做的,但发现执行tf.exe很慢。最后,在运行脚本之前简单地检查整个源树要快得多。我使用PowerGUI脚本编辑器来调试和运行PowerShell脚本。
$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
}
}
#3
I don't if it's going to be helpful in your case.
如果它对你的情况有帮助我不会。
- Modify the namespace, VS IDE will show you little red rectangular at the end of the namespace.
- Press Ctrl+. and select the option you like most.
修改命名空间,VS IDE将在命名空间的末尾显示小红色矩形。
按Ctrl +。并选择您最喜欢的选项。
If you're using tool like ReSharper, click on the namespace and press Ctrl+R. Follow instruction in Rename Namespace dialog box.
如果您使用的是ReSharper之类的工具,请单击命名空间并按Ctrl + R.按照“重命名命名空间”对话框中的说明操作
#4
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
看起来你不应该在全局搜索和替换完全限定名称时遇到太多麻烦。所以在oldspace :: myclass上进行搜索并将其替换为newspace :: myclass。在文件顶部添加行也不是很难,你可以替换
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.
当然,这样的方法存在一些风险。很容易引起自己微妙的问题。
#5
Another reason to switch to c sharp. Refactoring of source code built in for free in VS 2008.
切换到c锐利的另一个原因。重构VS 2008中免费内置的源代码。