开发者

Replace usage of a class in a specific project

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"开发者_C百科 feature, group by project and change the usages one by one. Is there an easier/automated way to do this?


Structural Search and Replace is your friend here. You get to it through ReSharper | Find | Search with Pattern....

  • Define a placeholder of type X, name it TX.
  • Make your Find pattern $TX$
  • Specify to "Look in" "Project"
  • As a check, carry out the search - you should see all the usages of X
  • Click the Replace toggle button
  • Make the Replace pattern the fully-qualified name of Y
  • Click Replace

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 named X, are renamed.

If you would prefer to end up with

using A.B.C;

/* later */
Y obObject = ...

rather than

A.B.C.Y myObject

I think you can achieve this through:

  1. In ReSharper | Options, Tools | Code Cleanup, define a new profile which has just "Optimize 'using' directives" checked
  2. In ReSharper | Options, Languages | C# | Namespace Imports add A.B.C to "Namespaces that should always be imported`
  3. Run ReSharper | Tools | Cleanup Code using the profile you just defined
  4. Tidy up by removing A.B.C from the namespaces list you added it to

although this will also clean up all other usings, which might make the version control diff a bit larger than you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜