C# 如何在c中实现这个接口#

C# 如何在c中实现这个接口#,c#,interface,C#,Interface,我有这个界面(简化) 我实现了如下界面(简化): 公共类ImageSourceViewModel:IImageSourceViewModel { 公共IObservableCollection行viewmodel{get;set;} } 但我得到一个错误,实现中的LinesViewModel无法在接口中实现LinesViewModel,因为它具有不同的返回类型 如何修复此问题?将LineViewModel作为返回类型,因为实现必须与如下所示的接口匹配 public interface IIm

我有这个界面(简化)

我实现了如下界面(简化):

公共类ImageSourceViewModel:IImageSourceViewModel
{
公共IObservableCollection行viewmodel{get;set;}
}
但我得到一个错误,实现中的LinesViewModel无法在接口中实现LinesViewModel,因为它具有不同的返回类型


如何修复此问题?

将LineViewModel作为返回类型,因为实现必须与如下所示的接口匹配

 public interface IImageSourceViewModel : INotifyPropertyChanged
{
    /// <summary>
    /// Gets or sets the lines view model.
    /// </summary>
    IObservableCollection<LineViewModel > LinesViewModel { get; set; }

 }
公共接口IImageSourceViewModel:INotifyPropertyChanged
{
/// 
///获取或设置线视图模型。
/// 
IObservableCollection LinesViewModel{get;set;}
}
更新:-

正如@Mike所建议的,您可以使用泛型来满足您的期望,即类不应该了解LineViewModel:-

public interface IImageSourceViewModel<T> : INotifyPropertyChanged where T : ILine
{
/// <summary>
/// Gets or sets the lines view model.
/// </summary>
IObservableCollection<T> LinesViewModel { get; set; }

}
公共接口IImageSourceViewModel:INotifyPropertyChanged其中T:ILine
{
/// 
///获取或设置线视图模型。
/// 
IObservableCollection LinesViewModel{get;set;}
}

您的实现必须与@Neels answer中提到的接口完全匹配

如果需要更改IObservableCollection的类型,则需要泛型

比如:

public interface IImageSourceViewModel<T> : INotifyPropertyChanged where T : ILine
{
/// <summary>
/// Gets or sets the lines view model.
/// </summary>
IObservableCollection<T> LinesViewModel { get; set; }

}


public class ImageSourceViewModel : IImageSourceViewModel<LineViewModel>
{

  public IObservableCollection<LineViewModel> LinesViewModel { get; set; }
}
公共接口IImageSourceViewModel:INotifyPropertyChanged其中T:ILine
{
/// 
///获取或设置线视图模型。
/// 
IObservableCollection LinesViewModel{get;set;}
}
公共类ImageSourceViewModel:IIImageSourceViewModel
{
公共IObservableCollection行viewmodel{get;set;}
}

但是我的类(ImageSourceViewModel)需要列表位于LineViewModel中。事实上,LineViewModel是一个ILine,为什么我会出现这个错误?但我不知道该接口对LineViewModel了解多少,它应该只知道接口ILine。如何确保t是实现ILine的?关键是ImageSourceViewModel的用户知道IImageSourceViewModel和ILine,他们不知道实现。他们需要将LinesViewModel用作ILine的集合。
 public interface IImageSourceViewModel : INotifyPropertyChanged
{
    /// <summary>
    /// Gets or sets the lines view model.
    /// </summary>
    IObservableCollection<LineViewModel > LinesViewModel { get; set; }

 }
public interface IImageSourceViewModel<T> : INotifyPropertyChanged where T : ILine
{
/// <summary>
/// Gets or sets the lines view model.
/// </summary>
IObservableCollection<T> LinesViewModel { get; set; }

}
public interface IImageSourceViewModel<T> : INotifyPropertyChanged where T : ILine
{
/// <summary>
/// Gets or sets the lines view model.
/// </summary>
IObservableCollection<T> LinesViewModel { get; set; }

}


public class ImageSourceViewModel : IImageSourceViewModel<LineViewModel>
{

  public IObservableCollection<LineViewModel> LinesViewModel { get; set; }
}