如何处理CS1721的时候我真的需要从两个类继承? [英] How to deal with CS1721 when I really need to inherit from two classes?
问题描述
在我的C#code我希望有一个 CustomIdentity
类,它继承 System.MarshalByRefObject
和 System.Security.Principal.GenericIndentity
类。
In my C# code I want a CustomIdentity
class that inherits from System.MarshalByRefObject
and System.Security.Principal.GenericIndentity
classes.
然而,当我尝试写这样的继承C#将与 CS1721错误对象一>说我不能直接从多个类继承。
However when I try to write such inheritance C# would object with CS1721 error saying I can't directly inherit from more than one class.
现在在这种情况下,它是很容易克服这一点 - 我会继承的IIdentity
添加 GenericIdentity
成员变量,重新实现所有的的IIdentity
通过该成员变量的方法。
Now in this case it's quite easy to overcome this - I'll inherit from IIdentity
, add GenericIdentity
member variable and reimplement all IIdentity
methods via that member variable.
但我怎么做的情况下,我想从两个类继承了一套方法,巨大的?
But how would I do in case I wanted to inherit from two classes with a huge set of methods?
推荐答案
您不知道。你不能。
这是不是在C#或.NET的支持。
That is not supported in C# or .NET.
您使用多个的接口实现的,和最多单(非 - 对象
)的继承。在大多数情况下,即使是单继承是已经被滥用; P
You use multiple interface implementation, and at most single (non-object
) inheritance. In most cases, even single inheritance is vastly overused ;p
要吸收的方法,你就必须使用某种类型的封装。扩展方法的也的提供方式来分享类型的方法,完整。
To absorb the methods, you'd have to use some kind of encapsulation. Extension methods also provide a way to share methods between types, for completeness.
这篇关于如何处理CS1721的时候我真的需要从两个类继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!