替换特定项目中类的使用
我正在使用 VS2010 和 Resharper 5.1。
我有两个项目,一个库项目(Lib)和一个应用程序项目(App),App引用Lib。
Lib 包含一个类 X,该类在 App 和 Lib 中都使用。在应用程序中,我现在想用类 Y 替换 X 的所有用法。在我的例子中,Y 与 X 无关,但如果它使重构更容易,它可以从 X 继承。
我目前计划使用“查找用法”功能,按项目分组并逐一更改用法。有没有更简单/自动化的方法来做到这一点?
I'm using VS2010 with Resharper 5.1.
I have two projects, a library project (Lib) and an application project (App), App references Lib.
Lib contains a class X which is used in both App and Lib. In App i now want to replace all usages of X with class Y. In my case Y is unrelated to X, but it could inherit from X if it makes refactoring easier.
My current plan to use the "Find usages" feature, group by project and change the usages one by one. Is there an easier/automated way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
结构搜索和替换是您的朋友。您可以通过 ReSharper | 获取它。查找|使用模式搜索...。
X
类型的占位符,将其命名为TX
。$TX$
X
的所有用法Y
的完全限定名称ReSharper 将向您显示它想要替换的所有提及的
X
- 确保选中顶部的复选框选中该树,然后单击“替换”。编辑
确实是通过在查找模式中使用类型占位符来确保仅引用类型
X
,而不是任何其他名为X
的内容,都会被重命名。如果您希望最终得到的结果
而不是
我认为的,您可以通过以下方式实现:
工具| 选中“优化‘使用’指令”。
ReSharper | 中 选项
、语言 | C# |命名空间导入
将ABC
添加到“应始终导入的命名空间”ReSharper | Tools | Cleanup Code
进行整理>ABC
从您添加到的名称空间列表中删除,尽管这也会清除所有其他
using
,这可能会使版本控制差异比您想要的要大一些。Structural Search and Replace is your friend here. You get to it through
ReSharper | Find | Search with Pattern...
.X
, name itTX
.$TX$
X
Y
ReSharper will show you all the mentions of
X
it wants to replace - ensure the checkbox at the top of the tree is checked, then click Replace.edit
It is indeed by using a type placeholder in our Find pattern that ensure that only references to the type
X
, rather than anything else namedX
, are renamed.If you would prefer to end up with
rather than
I think you can achieve this through:
ReSharper | Options
,Tools | Code Cleanup
, define a new profile which has just "Optimize 'using' directives" checkedReSharper | Options
,Languages | C# | Namespace Imports
addA.B.C
to "Namespaces that should always be imported`ReSharper | Tools | Cleanup Code
using the profile you just definedA.B.C
from the namespaces list you added it toalthough this will also clean up all other
using
s, which might make the version control diff a bit larger than you want.