我有一个接口如下:
public interface IPageViewModel<T> where T : class
{
string ViewName { get; set; }
T SelectedItem { get; set; }
List<T> ItemsList { get; set; }
}
然后,我有两个类:
internal class TestViewModel : IPageViewModel<INotifyPropertyChanged> //let's skip the fact that T is supposed to be a class while it somehow compiles and works with an interface...
internal class HardwareViewModel : IPageViewModel<Hardware>
Hardware
在哪里:
public class Hardware : NotificationObject
NotificationObject
是:
public class NotificationObject : INotifyPropertyChanged
最后,我有一个课程如下:
internal class NavigationViewModel
{
public List<IPageViewModel<INotifyPropertyChanged>> PageViewModelsList { get; set; } = new List<IPageViewModel<INotifyPropertyChanged>>();
public NavigationViewModel()
{
PageViewModelsList.Add(new TestViewModel());
PageViewModelsList.Add(new HardwareViewModel()); //error
}
}
现在,问题是:虽然构造函数中的第一行编译良好,但第二行抛出错误:cannot convert from ViewModels.HardwareViewModel to Helpers.IPageViewModel<System.Component.INotifyPropertyChanged>
。
但这毫无意义。Hardware
继承自实现INotifyPropertyChanged
NotificationObject
,所以IPageViewModel<Hardware>
===IPageViewModel<INotifyPropertyChanged>
。任何人都可以解释为什么会出现错误吗?
多亏了评论,我意识到在这里引起这些问题的主题称为"方差"。因此,在阅读了一些有关它的信息后,我决定采用此解决方案:
public interface IPageViewModel
{
string ViewName { get; set; }
}
但是,如果有人想保留这些字段并保持其接口协变,则必须如下所示:
public interface IPageViewModel<out T> where T : class
{
string ViewName { get; set; }
T SelectedItem { get; }
IEnumerable<T> ItemsList { get; }
}