2016-09-22 83 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; } 
    } 
}