2016-09-22 79 views
4

我有一个需求,我有一个类的所有派生从一个基类。基类包含也从相同基类派生的子类的列表。C#如何在派生类中添加属性设置器?

所有类都需要能够获得可以从类本身获得的特定值 - 或者 - 它的父类取决于派生类是什么。

我看过使用方法而不是属性,但我也想让这些值可用于.NET报表组件,它可以直接访问报表引擎中的公开属性,因此这排除了方法的使用。

我的问题是,这将是“最佳实践”实现在DerivedClass二传手没有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; } 
    } 
} 

我无法添加受保护的setter(如下)中BaseClass,因为我无法更改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的或其他类物业的能力。

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

其他建议?

+1

请参阅http://stackoverflow.com/questions/6749022/adding-a-setter-to-a-derived-interface(不完全是重复的)。 –

回答

5

你可以使用'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; } 
    } 
} 
+0

<新增错误> – Woodjitsu

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