简体   繁体   English

我可以使用扩展方法来实现接口吗?

[英]Can I use Extension Methods to implement an Interface?

I would like a class to implement an interface, I do not want to change the original class (that would add undesired dependecies). 我想要一个类来实现一个接口,我不想改变原来的类(这会添加不需要的依赖)。

I know I could inherit from the class and make it's child implement these methods, but then I am faced with a problem how to convert the parent class (that come from the data / ORM) to this presentation class. 我知道我可以从类继承并让它的子实现这些方法,但是我遇到了如何将父类(来自data / ORM)转换为此表示类的问题。

If I implement all the interface required methods, will it count as being that interface or not ? 如果我实现所有接口所需的方法,它会被视为该接口吗?

No, it still won't count as implementing the interface. 不,它仍然不会被视为实现接口。

Extension methods are nothing more than a way of calling static methods in a different kind of way. 扩展方法只不过是一种以不同方式调用静态方法的方法。 They don't affect object identity, inheritance etc at all. 它们根本不会影响对象标识,继承等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM