.NET工具:提取接口与实现封装类 [英] .NET Tools: Extract Interface and Implement Wrapper Class

查看:502
本文介绍了.NET工具:提取接口与实现封装类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个工具,可以生成提取物和生成现有类的接口?

Is there a tool that can generate extract and generate interfaces for existing classes?

我知道的的Visual Studio 的将提取的接口现有类。不过,我也想生成实现该功能的包装类。

I know Visual Studio will extract an Interface for an existing class. However, I would also like to generate a wrapper class that implements that functionality.

我相信这将极大帮助进行单元测试。

I believe this would help tremendously for unit testing.

示例现有类:

public class ThirdPartyClass
{
   public void Method1(){}
   public void Method2(){}
}

可以由Visual Studio(提取接口)产生:

public interface IThirdPartyClass
{
   void Method1();
   void Method2();
}



我想更进一步:

public class ThirdPartyClassWrapper : IThirdPartyClass
{
   private tpc = new ThirdPartyClass();
   public void Method1()
   {
       tpc.Method1();
   }
   public void Method2()
   {
       tpc.Method2();
   }
}



更新:

这将是静态类特别有用。由于莫滕指出,我可以只使用一个存根,但是,我想破了,如果有可能我的耦合。

This would be especially useful for static classes. As Morten points out I can just use a stub, however, I would like to break up my coupling if possible.

推荐答案

找到,变通的办法对非密封类

Found a way around it for non-sealed classes.

1 - 从外部类继承

1 - Inherit from the external class

class MyWrapper : ExternalClass

2 - 提取接口,用于所有公共方法

2 - Extract interface for all public methods

class MyWrapper : ExternalClass, IExternalClass

3 - 从外部类中删除继承

3 - Remove the inheritance from the external class

class MyWrapper : IExternalClass

4 - 您将获得类名称的提示有关从没有被实现的接口成员。 Alt + Enter键就可以了,让ReSharper的自动实现它们。

4 - You will get a hint on the class name about members from the interface not being implemented. Alt + Enter on it and let Resharper automatically implement them

5 - 使用此代码模板包装性能

5 - Use this code template to wrap properties

    get { return $INNERCOMPONENT$.$NAME$; }
    set { $INNERCOMPONENT$.$NAME$ = value; }



6 - 使用此代码模板来包装方法

6 - Use this code template to wrap methods

return $INNERCOMPONENT$.$NAME$($SIGNATURE$);

这篇关于.NET工具:提取接口与实现封装类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆