【发布时间】:2016-05-17 12:46:44
【问题描述】:
以下代码给出了错误 “无法将类型 'Calendar.Month' 转换为 'System.Collections.Generic.List'”:
public class Month : List<Day>, INotifyPropertyChanged
{
public Month() { /* NOP */ }
public new void Add<Day>(Day pValue)
{
var list = (List<Day>)this;
list.Add(pValue);
}
}
我在这里研究了建议: Why does calling a method in my derived class call the base class method?
这里:
C# Call shadow method with generic cast
我想用 Month.Add(...) 方法覆盖 List.Add(...) 方法以将事件发送到目标 XAML 容器类,然后调用覆盖的 List.Add(... ) 方法。
顺便说一句,我使用的是 VS2012、.NET 4.5,并且该应用程序适用于 Win Phone 8(目前不支持更高版本 - 抱歉)。我之所以提到这一点,是因为我从经验中知道 Win Phone 8 和 Win 10 之间发生了很大变化。
【问题讨论】:
-
我确定您的意思是
base.Add(pValue);而不是list.Add(pValue); -
不使用继承为什么不使用组合?
-
为什么要从
List<T>继承而不是Collection<T>? -
一个月是一个列表吗?没那么多。支持@juharr。一个月有几天的列表。
-
我使用 List
而不是 Collection 因为 Laurent 和 Jaime 在他们的“数据绑定”讲座(第 40-49 分钟)中就是这样做的:mva.microsoft.com/en-US/training-courses/… 他们是错误?当数据绑定 ItemsControl 的内容时,List 是否不可用? PS。出于这个问题的目的,我将 Month 视为 List 。它旨在遵循 Laurent 和 Jaime 采取的方法。
标签: c# .net overriding super overloading