首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >泛型类型的泛型?

泛型类型的泛型?
EN

Stack Overflow用户
提问于 2014-11-17 12:05:24
回答 2查看 100关注 0票数 0

我有以下代码:

代码语言:javascript
复制
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)
    {
    }
}

有没有办法避免第三个参数?如果我试图使用:

代码语言:javascript
复制
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 >也总是正确的。

代码语言:javascript
复制
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; }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-11-17 12:12:57

避免在EntityListViewModel的声明中使用泛型类型。

在这种情况下,不需要用泛型声明对象是否总是实现IEntity接口。

代码语言:javascript
复制
public abstract class EntityListViewModel
                      : ViewModelBase, IEntityListViewModel<IEntity>

您还需要将该类中的任何引用更改为TEntity

代码语言:javascript
复制
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声明为

代码语言:javascript
复制
public abstract class ListPresenter<TView, TModel>
                      : Presenter<TView, TModel>
                                 where TView : IListView<TModel>
                                 where TModel : EntityListViewModel<IEntity>
票数 1
EN

Stack Overflow用户

发布于 2014-11-17 12:09:52

你不能把IListViewEntityListViewModel限制在IEntity上吗?

代码语言:javascript
复制
public abstract class ListPresenter<TView, TModel> : Presenter<TView, TModel>
    where TView : IListView<IEntity>
    where TModel : EntityListViewModel<IEntity>

您可能需要使IListViewEntityListViewModel协变:

代码语言:javascript
复制
public interface IListView<out T>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26972347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档