在基类之前调用​​派生类构造函数

时间:2018-03-14 08:07:56

标签: c# wpf inheritance constructor override

我需要在基类构造函数之前执行派生类构造函数。 我附加了在基类中使用虚拟对象的代码,需要在派生类中初始化。我们决定派生类中虚拟对象的类型,然后在我们拥有该对象的类型后为该对象赋值。 在这种情况下,如何在基类构造函数之前调用派生类构造函数。

public class BaseClass : UserControl, INotifyPropertyChanged
{
    public Path ConnIn;
    public Path ConnOut;

    public virtual ObjectBase BaseObject { get; set; }

    public void BaseClass(XmlElementConfig config)
    {
        this.BaseObject.Title = config.Title;
        this.BaseObject.GroupID = config.GroupID;
    }
}

public partial class DerivedClass : CanvasBase
{
    private Audio_MonitorAction audio_objectAction;

    public override ObjectBase BaseObject
    {
        get { return audio_objectAction; }
        set
        {
            audio_objectAction = (Audio_MonitorAction)value;
            NotifyPropertyChanged();
        }
    }

    public DerivedClass(XmlElementConfig config) : base(config)
    {
        InitializeComponent();
        audio_objectAction = new Audio_MonitorAction(createGuid);
    }
}

2 个答案:

答案 0 :(得分:1)

"在基类构造函数"之前执行派生类构造函数是不可能的

如果可能,将初始化移动到BaseObject属性(为什么它应该是虚拟的?)

public class BaseClass : UserControl, INotifyPropertyChanged
{
    public Path ConnIn;
    public Path ConnOut;

    private ObjectBase baseObject;
    public ObjectBase BaseObject
    {
        get { return baseObject; }
        set
        {
            baseObject = value;
            if (baseObject != null)
            {
                baseObject.Title = config.Title;
                baseObject.GroupID = config.GroupID;
            }
            NotifyPropertyChanged();
        }
    }

    XmlElementConfig config;
    public void BaseClass(XmlElementConfig config)
    {
        this.config = config;
    }
}
public partial class DerivedClass : CanvasBase
{
    private Audio_MonitorAction audio_objectAction;

    public DerivedClass(XmlElementConfig config) : base(config)
    {
        InitializeComponent();
        BaseObject = audio_objectAction = new Audio_MonitorAction(createGuid);
    }
}

答案 1 :(得分:1)

public class BaseClass : UserControl, INotifyPropertyChanged
{
    public Path ConnIn;
    public Path ConnOut;

    public virtual ObjectBase BaseObject { get; set; }

    public void BaseClass(XmlElementConfig config)
    {
        InitBase(config);
    }
    protected void InitBase(XmlElementConfig config)
    {
        if (BaseObject != null)
        {
            BaseObject.Title = config.Title;
            BaseObject.GroupID = config.GroupID;
        }
    }
}
public DerivedClass(XmlElementConfig config) : base(config)
{
    InitializeComponent();
    audio_objectAction = new Audio_MonitorAction(createGuid);
    InitBase(config);
}

或者你可以在BaseClass中调用虚函数并在派生类中覆盖它。

public class BaseClass : UserControl, INotifyPropertyChanged
{
    public Path ConnIn;
    public Path ConnOut;

    public virtual ObjectBase BaseObject { get; set; }

    public void BaseClass(XmlElementConfig config)
    {
        InitDerivedClass();
        if (BaseObject != null)
        {
            BaseObject.Title = config.Title;
            BaseObject.GroupID = config.GroupID;
        }
    }
    protected virtual void InitDerivedClass() {}
}
public partial class DerivedClass : CanvasBase
{
    private Audio_MonitorAction audio_objectAction;

    public override ObjectBase BaseObject
    {
        get { return audio_objectAction; }
        set
        {
            audio_objectAction = (Audio_MonitorAction)value;
            NotifyPropertyChanged();
        }
    }
    protected override ObjectBase InitDerivedClass
    {
        audio_objectAction = new Audio_MonitorAction(createGuid);
    }
    public DerivedClass(XmlElementConfig config) : base(config)
    {
        InitializeComponent();
    }
}