Using R# 6, one way to achieve this would be with this sequence of operations. Note that refactorings are available on both ReSharper | Refactor
on the menu bar, or (quicker) in context with Ctrl+Shift+R. There's still a bit of typing; I wouldn't be surprised if there were a way that didn't require you to type anything at all.
- Start with your Original Code in a file named
Dinner.cs
- With the cursor on
Eat
, user the Extract Interface
refactoring, naming the interface IEatService
- With the cursor on
IEatService
, Alt+Enter and create a derived type, accepting the offered name of EatService
, and Alt+Enter to fill in the method implementation body
- Here's where we have to type: In
Dinner.Eat()
, type _eatService.Eat();
. Note that _eatService
is red; Alt+Enter on it, choose 'Create field', change the offered type to IEatService
- With the cursor on the definition of
_eatService
, Alt+Enter and choose 'Initialize from ctor paramater, and then Alt+Enter again and accept the readonly
suggestion
Note that initialising _eatService
to null
is redundant; R# would let you know this.
We're now at your target code.
I think R# 7 has some new refactorings (such as Extract Class) which might make this quicker.