C#如何在派生类中添加属性setter?

时间:2016-09-22 11:19:20

标签: c# inheritance

我有一个要求,我有许多类都派生自一个基类。基类包含也来自同一基类的子类列表。

所有类都需要能够获得可以从类本身获得的特定值-OR-它的父级取决于派生类的内容。

我查看了使用方法而不是属性,但是我还希望将值提供给.NET报告组件,该组件直接访问报告引擎中公开的公共属性,因此不包括方法的使用。

我的问题是在DerivedClass中实现setter的“最佳实践”方法,而没有BaseClass中公开的setter

public class BaseClass
{
  private BaseClass _Parent;
  public virtual decimal Result
  {
    get { return ((_Parent != null) ? _Parent.Result : -1); } 
  }
}

public class DerivedClass : BaseClass
{
  private decimal _Result;
  public override decimal Result
  {
      get { return _Result; }
      // I can't use a setter here because there is no set method in the base class so this throws a build error
      //set { _Result = value;  }
  }
}

我无法在BaseClass中添加受保护的setter(如下所示),因为我无法在DerivedClass中更改访问修饰符。

public class BaseClass
{
  private BaseClass _Parent;
  public virtual decimal Result {
    get { return ((_Parent != null) ? _Parent.Result : -1); } 
    protected set { }
  }
}

public class DerivedClass : BaseClass
{
  private decimal _Result;
  public override decimal Result
  {
    get { return _Result; }
    // This setter throws a build error because of a change of access modifiers.
    //set { _Result = value; }
  }
}

我不想在BaseClass中使用setter添加成员变量,因为我不希望能够从BaseClass或从base派生的其他类设置属性。

public class BaseClass
{
  private BaseClass _Parent;
  protected Decimal _Result; // This could result in a lot of unnecessary members in BaseClass.

  public virtual decimal Result {
    get { return _Result; } 
    // Empty setter ahead :) This does nothing.
    // I could also throw an exception here but then issues would not be found until runtime
    // and could cause confusion in the future with new developers etc.
    set { }
  }
}

public class DerivedClass : BaseClass
{
  public override decimal Result
  {
    get { return base.Result; }
    set { base._Result = value; }
  }
}

其他建议?

3 个答案:

答案 0 :(得分:4)

您可以使用'new'关键字,但这将替换属性,您想要的覆盖是不可能的。

public class BaseClass
{
    private BaseClass _Parent;
    public virtual decimal Result
    {
        get { return ((_Parent != null) ? _Parent.Result : -1); }
    }
}

public class DerivedClass : BaseClass
{
    private decimal _Result;
    public new decimal Result
    {
        get { return _Result; }
        set { _Result = value;  }
    }
}

答案 1 :(得分:3)

如果您可以在 BaseClass 中添加受保护的 setter,您可以在 DerivedClass 中使用 new 关键字:

public class BaseClass  // ReadOnlyClass
{
    public int Value { get; protected set; }
}

public class DerivedClass : BaseClass
{
    public new int Value
    {
        get => base.Value;
        set => base.Value = value;
    }
}

答案 2 :(得分:0)

我最终改变了处理它的方式并将该集保留在基类中,而不是让基类getter / setter什么也不做,我抛出了NotImplemented / NotSupported异常。

public class BaseClass
{
  private BaseClass _Parent;
  public virtual decimal Result
  {
     get
     {
      if (Parent == null)
        throw new NotImplementedException("Result property not valid");

      return Parent.Result;
    }
    set
    {
      throw new NotSupportedException("Result property cannot be set here");
    }
  }
}

public class DerivedClass : BaseClass
{
  private decimal _Result;
  public override decimal Result
  {
    get { return _Result; }
    set { _Result = value;  }
  }
}