1 回答

TA貢獻(xiàn)1874條經(jīng)驗(yàn) 獲得超12個(gè)贊
你寫了:
public interface IGenericRepository<TEntity>
where TEntity: class
{ }
public class GenericRepository<TEntity>
where TEntity:class, IGenericRepository<TEntity>
{ }
這意味著TEntityin aGenericRepository<TEntity>必須是實(shí)體的存儲庫。
這種約束是合法的,但它通常用于類似
class SortedList<T> where T : IComparable<T>
也就是說,T 的排序列表要求 T 與其他 T 具有可比性。
我想你的意圖是
public class GenericRepository<TEntity> :
IGenericRepository<TEntity>
where TEntity:class
{ }
正確的?基類和接口的列表出現(xiàn)在約束之前;你把它放在約束中。
也就是說,類聲明是這樣的:
class ClassName<T> :
BaseClass,
IInterface1,
IInterface2
where
T : CONSTRAINT,
CONSTRAINT,
...
并且約束也必須以正確的順序出現(xiàn)。如果您對類聲明的語法有疑問,請閱讀 C# 規(guī)范。您似乎對事情發(fā)生的順序有些困惑。
- 1 回答
- 0 關(guān)注
- 90 瀏覽
添加回答
舉報(bào)