views:

30

answers:

1

When using Visual Studio it is easy to extract an interface from a class that I have written myself. I right click on the class and select 'Refactor' then select 'Extract Interface'.

Let's assume for a second that I wanted to create a ConfigurationManager wrapper and write some tests around it. A quick way to do that would be to extract an interface from ConfigurationManager by right clicking it, then 'Go To Definition' and then from inside the class select 'Refactor' then select 'Extract Interface'. Then I would simply create my wrapper class, inherit from my newly created interface, and then implement it and I have a great starting point for my wrapper class.

However, extracting an interface from any .NET system classes is not possible, probably because it's just meta data about the classes and not the classes themselves (or I am doing it wrong).

Is there an easy way to do what I am trying to accomplish? I want to ensure I am not wasting time typing what I don't need to be typing.

Thanks

+2  A: 

The problem is not so much to extract the interface - you could also do this 'by hand' But you have no way to tell the CLR that the System-defined Configuration manager implements this interface since this (meta-)information is stored in the framework assembly which you cannot modify.

EDIT:

To ease the 'extraction by hand' you can click with the right button on the type and select "Go to Definition". Visual Studio creates a class definition (without implementation) from the metadata. You can then use copy and paste into a new file. Anyway you'll still have to do some modifications by hand

  • Replace the class keyword by interface
  • remove all non-public methods/properties
  • remove the public and override access modifiers (they are invalid in an interface definition)

This can be done easily using search&replace. You'll even get the documentation strings with this approach.

MartinStettner
Right, but I wanted to know if there is a faster way to extract an interface from any .NET System class (like the ConfigurationManager) without doing it 'by hand'.
Thomas
Thanks Martin. Do you know if Resharper or any other VS plug-ins accommodate extracting classes from meta data?
Thomas
Sorry, I'm not aware of any existing plugin to do this although it shouldn't be too difficult to write one ...
MartinStettner