我有以下代码:
public abstract class ListPresenter<TView, TModel, TEntity> : Presenter<TView, TModel>
where TView : IListView<TModel>
where TModel : EntityListViewModel<TEntity>
where TEntity : class, IEntity
{
public ListPresenter(TView view, TModel model)
: base(view, model)
{
}
}有没有办法避免第三个参数?如果我试图使用:
public abstract class ListPresenter<TView, TModel> : Presenter<TView, TModel>
where TView : IListView<TModel>
where TModel : EntityListViewModel<IEntity>我得到:
错误3类型'PuntoVenta.ViewModels.SaleListViewModel‘不能用作泛型类型或方法’PuntoVenta.Present.ListPresenter‘中的类型参数'TModel’。没有从'PuntoVenta.ViewModels.SaleListViewModel‘到'PuntoVenta.ViewModels.EntityListViewModel’的隐式引用转换。C:\Users\Marc\Dropbox\Source\PointOfSale\PuntoVenta\Presentation\ListSalePresenter.cs 26 20 PuntoVenta.UI
即使是EntityListViewModel< IEntity >也总是正确的。
public abstract class EntityListViewModel<TEntity> : ViewModelBase, IEntityListViewModel<TEntity>
where TEntity : class, IEntity
{
private BindingSource Entities { get; set; }
private string searchQuery = string.Empty;
public EntityListViewModel()
{
SearchQuery = string.Empty;
Entities = new BindingSource();
}
public TEntity Selected
{
get { return Entities.Current as TEntity; }
set { Entities.Position = Entities.IndexOf(value); }
}
public string SearchQuery
{
get { return searchQuery; }
set
{
searchQuery = value;
NotifyProperty("SearchQuery");
}
}
public List<TEntity> DataSource
{
set
{
Entities.DataSource = value;
NotifyProperty("DataSource");
}
}
}
public interface IEntity
{
Guid Id { get; set; }
DateTime DateCreated { get; set; }
DateTime DateUpdated { get; set; }
}
public interface IListView<TModel> : IView<TModel>
where TModel : ViewModelBase
{
event EventHandler OnSearchQueryChanged;
event EventHandler OnSelectRequested;
}
public abstract class ViewModelBase : IModel
{
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyProperty(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public class SaleListViewModel : EntityListViewModel<SaleView>
{
private bool _ShowPendingOnly = false;
public SaleListViewModel()
{
}
public bool ShowPendingOnly
{
get { return _ShowPendingOnly; }
set
{
_ShowPendingOnly = value;
NotifyProperty("ShowPendingOnly");
}
}
public List<Customer> Customers { get; set; }
}发布于 2014-11-17 12:12:57
避免在EntityListViewModel的声明中使用泛型类型。
在这种情况下,不需要用泛型声明对象是否总是实现IEntity接口。
public abstract class EntityListViewModel
: ViewModelBase, IEntityListViewModel<IEntity>您还需要将该类中的任何引用更改为TEntity。
public IEntity Selected
{
get { return Entities.Current as IEntity; }
set { Entities.Position = Entities.IndexOf(value); }
}
public List<IEntity> DataSource
{
set
{
Entities.DataSource = value;
NotifyProperty("DataSource");
}
}此时,您可以将ListPresenter声明为
public abstract class ListPresenter<TView, TModel>
: Presenter<TView, TModel>
where TView : IListView<TModel>
where TModel : EntityListViewModel<IEntity>发布于 2014-11-17 12:09:52
你不能把IListView和EntityListViewModel限制在IEntity上吗?
public abstract class ListPresenter<TView, TModel> : Presenter<TView, TModel>
where TView : IListView<IEntity>
where TModel : EntityListViewModel<IEntity>您可能需要使IListView和EntityListViewModel协变:
public interface IListView<out T>https://stackoverflow.com/questions/26972347
复制相似问题