.NET工具:提取接口与实现封装类

编程入门 行业动态 更新时间:2024-10-19 04:23:59
本文介绍了.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工具:提取接口与实现封装类

本文发布于:2023-11-09 20:52:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573404.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:接口   工具   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!