danrok
danrok

Reputation: 51

Resharper extract interface from object passed to method

I have a question if Resharper can help me with below problem.

Let's say there is a class with many properties inside:

 public class TestClass {
      public string variableA {get; set;}
      public string variableB {get; set;}
      public int variableC {get; set;}
 }

then somewhere else we have a method that uses TestClass object

 public void TestMethod(TestClass classInstance) {
      classInstance.variableA = 'new value';
      classInstance.variableC = 1;
 }

of course this example is much simplified to the one I have, but I want somehow to extract interface that will have only

variableA
variableC

because then I want to pass it as a parameter to TestMethod. Can ReSharper do it automatically?

Upvotes: 0

Views: 92

Answers (2)

danrok
danrok

Reputation: 51

I did what I wanted and I want to share solution, or maybe better say small workaround.

  • first what I did was to create an interface that contained all properties from the TestClass.
  • I changed method signature to get this interface instead of class itself.
  • I turned on wide analysis in R# and checked which properties are not used and removed them from the interface.

Maybe someone will need this in future when refactoring. Anyway - thanks for your help!

Upvotes: 0

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73452

  • Right click the class
  • Select Refactor -> Extract -> Extract interface.

It takes you to extract interface window, in which you can select all the properties you want to extract it to a new interface.

In visual studio keyboard scheme shortcut happens to be ctrl + shift + R, x or select "Extract interface".

Once this refactoring is done, it is simply the matter of changing the method's formal parameter to use your interface as opposed to the concrete type.

Upvotes: 1

Related Questions