2

Recently, I've been having to wrap a lot of dot net framework classes for better unit test coverage. Instead of doing this by hand, I was wondering if there was a way to extract an interface from an existing system class in the dot net framework using a tool like Reshaper (or something similar).

Once I have the interface I dont mind implementing the concrete class myself. But it would be great if I could automatically generate the interface.

Chris Lees
  • 2,140
  • 3
  • 21
  • 41
  • https://www.jetbrains.com/resharper/documentation/help20/Refactoring/extractinterface.html – Daniel A. White Nov 17 '14 at 16:58
  • 1
    you could also use a mock/fake library. – Daniel A. White Nov 17 '14 at 16:58
  • 1
    The latest is [Extract Interface](https://www.jetbrains.com/resharper/webhelp/Refactorings__Extract_Interface.html), but won't help directly for a class from .NET Framework. But try navigating to the class source code and see if ReSharper will extract the interface from the online source or disassembled source. – John Saunders Nov 17 '14 at 16:59
  • @JohnSaunders I cant seem to navigate system class source anymore in Visual Studio 2013. The option is grayed out. I tried even enabling symbol servers in the VS options but nothing seems to bring back that feature. – Chris Lees Nov 17 '14 at 17:12
  • Don't sweat it. I just tried it with the `Controller` class from ASP.NET MVC. I was able to "Navigate To->Sources from Symbol Files", but Refactor This" only showed "Use base type where possible". – John Saunders Nov 17 '14 at 17:29
  • @JohnSaunders aww boo. Thanks for trying though! – Chris Lees Nov 17 '14 at 17:37
  • I use http://fakeiteasy.github.io/ it creates recursive fakes and is really easy to use – reggaeguitar Nov 17 '14 at 19:34
  • You could use the 'definition from metadata' to help create an interface from a .Net class a bit quicker than writing it completely by hand from scratch: Right-click on the class definition and click "Go To Definition". This will show you the class definition without any implementation. You can copy this and with a little bit of work convert it to an interface - change `class` to `interface`, remove access modifiers and inheritance modifiers. Find and replace will help with this. You may also need to remove attribute declarations. Repeat the parent class if the .NET class inherits any. – Dan Stevens Apr 22 '16 at 16:22
  • @reggaeguitar this is only at runtime. I'm looking for a tool that will generate a code file I can compile along with my application. – Chris Lees Apr 29 '16 at 17:49
  • @DanStevens This can take a lot of time depending on the size of the class. Also the way Visual Studio displays the code comments takes a lot of work to get them back to the correct way to do them. – Chris Lees Apr 29 '16 at 17:51

0 Answers0