我已經定義了以下接口:無法IEnumerable的<TEntity>轉換爲TCollection
public interface IReadOnlyRepositoryBase<TEntity, TKey, TCollection>
where TEntity : EntityBase<TKey>
where TCollection: IEnumerable<TEntity>
{
TCollection GetAll();
}
public interface IReadOnlyRepository<TEntity, TKey> :
IReadOnlyRepositoryBase<TEntity, TKey, IEnumerable<TEntity>>
where TEntity : EntityBase<TKey>
{ }
// there is also "ILazyReadOnlyRepository" where TCollection
// is IQueryable<T>..
現在,我不能在我的實現返回IEnumerable<TEntity>
因爲IEnumerable<TEntity>
似乎並不可轉換爲TCollection
。
// basic repository impl for NHibernate
public abstract class NHibernateReadOnlyRepositoryBase<TEntity, TKey, TCollection>
: IReadOnlyRepositoryBase<TEntity, TKey, TCollection>
where TEntity : EntityBase<TKey>
where TCollection : IEnumerable<TEntity>
{
public TCollection GetAll()
{
// doesn't work...
return _session.QueryOver<TEntity>().List();
}
至於我可以看到,該方法返回一個IList<T>
它實現IEnumerable<T>
,所以這顯然應該工作?我怎樣才能達到我想要的?
應該如何工作? 'TCollection'也可以是'List',在這種情況下,你可以將'IList '投射到'List '。那是不可能的。 –
2014-08-29 15:28:04