0

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?

2 Answers 2

1
  • 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.

Sign up to request clarification or add additional context in comments.

3 Comments

I saw that of course, but this extract interface window does not select all variables from that object that are used within method. This is my concern - if it can be automatically selected, as number of properties which are used are not so small and I want to perform this operation in many places.
So, in other words, no, it can't do it automatically.
@Konrad This is the best you can get AFAIK.
0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.