我有一個靜態類DataSource
從文件拉動請求的數據,並返回它作爲一個List<IInfrastructureEntity>.
在TestRepository
(下圖),這是我在通用TEntity
,確定其類別類型並從DataSource
中提取相應的數據,或者至少嘗試。相反,我在每個return語句上得到以下編譯時錯誤。即使任何TEntity
必然實施IInfrastructureEntity
。轉換列表<IInfrastructureEntity>列出<TEntity>其中通用類型TEntity不一定實現該接口
無法隱式轉換類型「System.Collections.Generic.List<IInfrastructureEntity>
」到「System.Collections.Generic.List<TEntity>
」
如何明確地作出這樣的轉變?
public class TestRepository<TEntity> : IRepository<TEntity> where TEntity : IInfrastructureEntity
{
public List<TEntity> GetData()
{
TEntity checkType = default(TEntity);
if (checkType is Member) return DataSource.Members;
if (checkType is MenuItem) return DataSource.MenuItems;
if (checkType is CRAWApplication) return DataSource.CRAWApplications;
if (checkType is CRAWEntitlement) return DataSource.CRAWEntitlements;
if (checkType is FOXGroup) return DataSource.FOXGroups;
throw new NotSupportedException(checkType.ToString() + " is not yet supported");
}
public List<TEntity> FindBy(Expression<Func<TEntity, bool>> predicate)
{
return GetData().AsQueryable().Where(predicate);
}
}
Oy!那是什麼?謝謝。 – Joe
@Joe歡迎您!我很高興我的幫助。 – Christos