本文介绍了如何延长两个类以同样的方式,而不复制和粘贴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写了一个自绘的TabControl
,但我们的项目还使用了 TabWorkspace
这源于的TabControl
。目前,我有
I've written an owner-drawn TabControl
, but our project also uses TabWorkspace
which derives from TabControl
. At the moment, I've got
public class OurTabControl : TabControl
{
// some code that overrides protected methods
}
public class OurTabWorkspace : TabWorkspace
{
// the same code
}
我想只有共享code出现在一个地方,这样我们就不必维护两个副本。这是可能在C#中,如果是的话,怎么办?
I'd like to only have the shared code appear in one place, so we don't have to maintain two copies. Is this possible in C#, and if so, how?
推荐答案
您可以使用一个接口和编写扩展方法它做什么。
You could do it using an interface and writing an extension method.
class OurTabControl: IBehavior
class OurTabWorkspace: IBehavior
interface IBehavior
{
}
static class BehaviorExtensions
{
static [returntype] RepeatedBehavior(this IBehavior behavior)
{
//repeated code here
}
}
这篇关于如何延长两个类以同样的方式,而不复制和粘贴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!