如何在没有复制粘贴的情况下以相同的方式扩展两个类?

时间:2022-08-22 21:17:46

I've written an owner-drawn TabControl, but our project also uses TabWorkspace which derives from TabControl. At the moment, I've got

我编写了一个所有者绘制的TabControl,但我们的项目也使用TabConspace,它派生自TabControl。此刻,我有

public class OurTabControl : TabControl
{
     // some code that overrides protected methods
}

public class OurTabWorkspace : TabWorkspace
{
    // the same code
}

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?

我只希望共享代码出现在一个地方,所以我们不必保留两份副本。这可能在C#中,如果是这样,怎么样?

4 个解决方案

#1


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
   } 
}

#2


Perhaps refactor the shared code into a single set of static methods?

也许将共享代码重构为一组静态方法?

#3


You could create an internal helper class that contains the common code and reference it as a private member in OurTabControl and OurTabWorkspace. Since TabWorkspace extends TabControl you should be able to take advantage of polymorphism and pass TabControl parameters to methods in the helper class. You would still need to override any common methods in both of your extended classes and call the helper class methods and/or base methods from there.

您可以创建一个包含公共代码的内部帮助器类,并将其作为OurTabControl和OurTabWorkspace中的私有成员引用。由于TabWorkspace扩展了TabControl,您应该能够利用多态性并将TabControl参数传递给helper类中的方法。您仍然需要覆盖两个扩展类中的任何常用方法,并从那里调用辅助类方法和/或基本方法。

#4


How about using a partial class? Or, what about defining all the shared methods in one class and have both inherited classes instantiate it.

如何使用部分类?或者,如何在一个类中定义所有共享方法,并让两个继承类实例化它。

#1


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
   } 
}

#2


Perhaps refactor the shared code into a single set of static methods?

也许将共享代码重构为一组静态方法?

#3


You could create an internal helper class that contains the common code and reference it as a private member in OurTabControl and OurTabWorkspace. Since TabWorkspace extends TabControl you should be able to take advantage of polymorphism and pass TabControl parameters to methods in the helper class. You would still need to override any common methods in both of your extended classes and call the helper class methods and/or base methods from there.

您可以创建一个包含公共代码的内部帮助器类,并将其作为OurTabControl和OurTabWorkspace中的私有成员引用。由于TabWorkspace扩展了TabControl,您应该能够利用多态性并将TabControl参数传递给helper类中的方法。您仍然需要覆盖两个扩展类中的任何常用方法,并从那里调用辅助类方法和/或基本方法。

#4


How about using a partial class? Or, what about defining all the shared methods in one class and have both inherited classes instantiate it.

如何使用部分类?或者,如何在一个类中定义所有共享方法,并让两个继承类实例化它。