0

我在两个独立的库中有两个类(一个 VB,一个 C#):

Public Class Item
    ...
    Public Overridable ReadOnly Property TotalPrice() As String
    Get
        Return FormatCurrency(Price + SelectedOptionsTotal())
    End Get
End Property
End Class

public class DerivedItem : Item {
    ...
   public new Decimal TotalPrice
    {
        get { return Price + OptionsPrice; }
    }
}

如您所见,DerivedItem.TotalPrice阴影遮住了Item.TotalPrice属性。但是,当尝试检索该DerivedItem.TotalPrice值时,我仍在获取基础对象的TotalPrice值。

为什么DerivedItem' 的属性没有被归还?

编辑

我真的发现了问题!我在通过 AJAX 返回的 JSON 字符串中得到了错误的值。事实证明,TotalPrice 被正确返回,它只是被稍后在 JSON 字符串中进行的阴影属性调用覆盖。那么,我的新问题是如何防止阴影属性被序列化?

(此问题已在此处重新调整范围)

4

3 回答 3

2

这可能取决于您如何实例化对象。

例如:

DerivedItem i = new DerivedItem();
i.TotalPrice();

将调用阴影版本。

然而:

Item i = new DerivedItem();
i.TotalPrice();

实际上会调用基地。

这是一个很好的解释。

当然,如果可能的话,我会避免阴影.... :-)

于 2011-04-06T00:57:48.483 回答
0

<NonSerialized()>在基类属性上设置属性是否有效?

于 2011-04-15T09:07:23.230 回答
0

您是从对基本类型的引用中引用 TotalPrice 吗?

Item item = new DerivedItem;
string s = item.TotalPrice;
于 2011-04-06T00:54:30.987 回答