3

I need to replace all the ocurances of obj.Method1() to obj.Method2() where obj is an instances of the same class. Does ReSharper or VS2010 allow this?

Sergey Metlov
  • 25,747
  • 28
  • 93
  • 153

3 Answers3

7

You could write a ReSharper Replace Pattern (ReSharper->Tools->Pattern Catalog, Add Pattern) like so:

enter image description here

where the type of expression obj needs to be changed to your class (that contains Method1).

Then press Save and thereafter press Search now to get all Method1() calls:

enter image description here

Then click Replace and all Method1 calls are type-safely replaced to Method2.

brgerner
  • 4,287
  • 4
  • 23
  • 38
4

I'd cheat - Do a 3-point symbolic rename (Right-click->Rename on a method name)

  • Method1 -> Temp
  • Method2 -> Method1
  • Temp -> Method2

None of this will change code functionality but will update all your code to use the correct name, except the methods themselves are now named incorrectly - simply rename the methods to by hand and voila - A little bit hacky but fast and effective.

This does rely on all method calls being in managed code (so that VS knows how/what to rename). If you have XML comments, C# rename handles this well but VB doesn't - I assume since you mention ReSharper, you're using C#?

This also assumes the method signatures are identical (if not, get ready for a lot of copy/pasting)

Basic
  • 26,321
  • 24
  • 115
  • 201
  • ReSharper's support for VB is getting close to it's support for C#, at least in a lot of ways. – dlras2 Apr 26 '12 at 15:30
  • I thought about this but was hoping there was something like "Replace method call" action either in VS or R# :) – Sergey Metlov Apr 26 '12 at 15:36
  • @Dan I wsan't aware of that - been a while since I've tried it in VB, I'll have to give it another spin – Basic Apr 26 '12 at 19:56
0

Make body of Method1 like this

public void Method1(...) { return Method2(...); }

And invoke refactoring "Inline method" on Method1.

derigel
  • 3,218
  • 2
  • 19
  • 31