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" 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... ReSharper | Find | Search with Pattern... 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:

  • In ReSharper | Options ReSharper | Options , Tools | Code Cleanup Tools | Code Cleanup , define a new profile which has just "Optimize 'using' directives" checked
  • In ReSharper | Options ReSharper | Options , Languages | C# | Namespace Imports Languages | C# | Namespace Imports Languages | C# | Namespace Imports add ABC to "Namespaces that should always be imported`
  • Run ReSharper | Tools | Cleanup Code ReSharper | Tools | Cleanup Code ReSharper | Tools | Cleanup Code using the profile you just defined
  • Tidy up by removing ABC from the namespaces list you added it to
  • although this will also clean up all other using s, which might make the version control diff a bit larger than you want.

    链接地址: http://www.djcxy.com/p/55222.html

    上一篇: DDMS(Android调试监视器)不会列出Galaxy S II上的进程

    下一篇: 替换特定项目中的类的使用