Replace class use in a specific project

I am using VS2010 with Resharper 5.1.

I have two projects, a library project (Lib) and an application project (application), links to Lib applications.

Lib contains the class X, which is used both in applications and in Lib. In the App, I now want to replace all applications of X with the Y class. In my case, Y is not related to X, but it can inherit from X if it simplifies refactoring.

My current plan is to use the Find Uses feature, project teams, and one-off customs. Is there an easier / automatic way to do this?

+7
source share
1 answer

Structural search and replacement 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 Search Template $TX$
  • Specify "Look in" "Project"
  • As a check, do a search - you should see all the ways to use X
  • Click "Replace Switch"
  • Make the Replace template fully qualified with the name Y
  • Click Replace

ReSharper will show you all the references to X that it wants to replace - check the box at the top of the tree, and then click Replace.

change

Indeed, using the template placeholder in our search template, make sure that only references to type X renamed, and not to everything called X

If you prefer to finish

 using ABC; /* later */ Y obObject = ... 

but not

 ABCY myObject 

I think you can achieve this through:

  • In ReSharper | Options ReSharper | Options , Tools | Code Cleanup Tools | Code Cleanup define a new profile that has just โ€œoptimizedโ€ using proven โ€œdirectivesโ€.
  • 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"
  • Launch ReSharper | Tools | Cleanup Code ReSharper | Tools | Cleanup Code ReSharper | Tools | Cleanup Code using just defined profile.
  • Remove ABC from the list of names you added to

although it will also clear everyone else using s, which may result in version control being a little more than you want.

+4
source

All Articles