开发者

Extracting an interface from .NEt System classes

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 as开发者_开发百科sume 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜