ASP.NET:影子问题 [英] ASP.NET: Shadowing Issues
问题描述
我在两个独立的库中有两个类(一个VB,一个C#):
I have two classes in two separate libraries (one VB, one 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
值.
As you can see, the DerivedItem.TotalPrice
shadows the Item.TotalPrice
property. However, when trying to retrieve the DerivedItem.TotalPrice
value, I'm still getting the base object's TotalPrice
value.
为什么不返回DerivedItem
的属性?
编辑
我实际上已经找到了问题!我在通过AJAX返回的JSON字符串中得到了错误的值.事实证明,正确返回了TotalPrice
,只是稍后在JSON字符串中进行的阴影属性调用将其覆盖.那么,我的新问题是如何防止阴影属性被序列化?
I've actually found the problem! I am getting the wrong value in the JSON string being returned via AJAX. It turns out that the TotalPrice
is being returned correctly, it's just being overwritten by the shadowed property call being made later in the JSON string. My new question, then, is how to I prevent the shadowed property from being serialized?
(此问题已在此处重新界定) strong>
(This question has been rescoped here)
推荐答案
它可能取决于您如何实例化对象.
It may depend on how you are instantiating the object.
例如:
DerivedItem i = new DerivedItem();
i.TotalPrice();
将调用阴影版本.
但是:
Item i = new DerivedItem();
i.TotalPrice();
实际上会呼叫基地.
当然,如果可能的话,我会避免遮挡....:-)
Of course if at all possible I would avoid shadowing.... :-)
这篇关于ASP.NET:影子问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!