扩展已经继承另一个类的类

本文关键字:另一个 继承 扩展 | 更新日期: 2023-09-27 18:24:22

我继承了一些System.Windows.Forms-Control(大约10个)。每个控件都有一些自定义扩展,但每个控件的大多数扩展都是相同的。

实际上,我必须分别为它们编写相同的功能。这是大量的复制+粘贴,而且很难维护。

class MyButton : Button
{
    //this is only in MyButton
    public int ButtonProperty { get; set; }
    public object Property1 { get; set; }
    public object Property2 { get; set; }
    public void MakeInvisible()
    {
        this.Visible = false;
    }
}
class MyLabel : Label
{
    //this is only in MyLabel
    public bool LabelProperty { get; set; }
    //same propertys and methods as in MyButton
    public object Property1 { get; set; }//copy+paste
    public object Property2 { get; set; }//copy+paste
    public void MakeInvisible()//copy+paste
    {
        this.Visible = false;
    }
}

我正在寻找一种扩展所有派生类的方法,就像使用interface或扩展方法一样但我也想拥有属性并访问基类(Control

这就是我的梦想:

class MyButton : Button, MyExtension
{   
    //this is only in MyButton
    public int ButtonProperty { get; set; }
}
class MyLabel : Label, MyExtension
{
    //this is only in MyLabel
    public bool LabelProperty { get; set; }
}
//Extension for all classes inherited from Control
class MyExtension : Control
{
    public object Property1 { get; set; }
    public object Property2 { get; set; }
    public void MakeInvisible()
    {
        this.Visible = false;
    }
}

扩展已经继承另一个类的类

想法:

  1. 为公共属性创建一个新类型

  2. 给每个控件一个类型的属性

实现:

// TypeConverter required for PropertyGrid in design mode
// found here: http://stackoverflow.com/a/6107953/1506454
[TypeConverter(typeof(ExpandableObjectConverter))]
public class MyExtension
{
    // need reference to control to work with in methods
    private Control _c;
    public MyExtension(Control c)
    {
        _c = c;
    }
    // can be inhereted for different controls, if necessary
    public string Property1 { get; set; }
    public string Property2 { get; set; }
    public void MakeInvisible()
    {
        _c.Visible = false;
    }
}
// common interface of extended controls
public interface IExtended
{
    MyExtension Extra { get; }
}
// MyButton implements extended interface
public class MyButton : Button, IExtended
{
    public MyButton()
    {
        // create extended properties for button
        Extra = new MyExtension(this);
    }
    // for designer serialization support
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public MyExtension Extra { get; private set; }
    //this is only in MyButton
    public int ButtonProperty { get; set; }
}
// common extension methods
public static class MyControlHelper
{
    public static void MakeInvisible<TControl>(this TControl control) where TControl : Control, IExtended
    {
        control.Extra.MakeInvisible();
    }
    public static void Rename<TControl>(this TControl control) where TControl : Control, IExtended
    {
        control.Text = control.Extra.Property1;
    }
}

C#不支持多重继承你应该试试这样的东西——MyButton : MyExtension;MyExtension : Button。在这种情况下,您将使用MyExtension和Button类来扩展MyButton类。

您可以为此目的使用扩展方法

public static class ControlHelper
{
    public static void MakeInvisible(this Control c)
    {
        c.Visible = false;
    }
}

并像这个一样使用它

var mb = new MyButton();
mb.MakeInvisible();
var ml = new MyLabel();
ml.MakeInvisible();

通过使用这种方法,您可以为基类生成扩展方法,并在派生类中使用它。

您可以使用composition而不是从ButtonLabel继承。

class MyExtension
{
    protected Control control;
    public MyExtension(Control control)
    {
        this.control = control;
    }
    public object Property1 { get; set; }
    public object Property2 { get; set; }
    public void MakeInvisible()
    {
        this.control.Visible = false;
    }
}
class MyButton : MyExtension
{
    public MyButton(Button button):base(button){}
    public int ButtonProperty { get; set; }
}
class MyLabel : Label
{
    public MyButton(Label label):base(label){}
    public bool LabelProperty { get; set; }
}

如果您不想创建MyExtension的任何实例,您甚至可以将其设置为abstract。这里的主要区别是,您必须创建一个ButtonLabel才能传入,并且您可能希望将它们公开为MyButtonMyLabel的属性,以便了解它们的属性。

如果您需要利用扩展控件的protected方法和属性,那么您就不走运了,如果没有广泛的复制和粘贴,就无法实现您想要的内容。

如果您只需要访问公共方法和属性,那么按照以下方式进行操作如何:

public interface IControlExtension
{
    Foo MyProperty { get; set; } 
    Blah MyMethod();
}
public abstract class ControlExtension: IControlExtension
{
     private Control owner;
     private ControlExtension(Control owner)
     {
         Debug.Assert(owner != null);
         this.owner = owner;
     }
     public static IControlExtension GetControlExtension(Control c)
     {
          if (c is Button ||
              c is Label)
          {
              return new SimpleControlExtension(c);
          }
          if (c is Panel || ...
          {
              return new ContainerControlExtension(c);
          }  
     }
     public abstract Foo MyProperty { get; set; }
     public abstract Blah MyMethod();
     private class SimpleControlExtension: ControlExtension
     {
          public override Foo MyProperty { .... }
          public override Blah MyMethod { .... 
     }
     private class ContainerControlExtension: ControlExtension
     {
          public override Foo MyProperty { .... }
          public override Blah MyMethod { .... }
     }
}

现在,在所有扩展控件中,复制和粘贴代码是最小的:

public class MyButton : Button
{   
    public MyButton()
    {
        ....
        var controlExtension = ControlExtension.GetControlExtension(this);
    }
    public IControlExtension { get { return controlExtension; } }
}