2016-07-23 56 views
-1

在最小的奇怪,但我相信存在一個解釋... 我有一個接口(IRepository)與6個方法要實現的類。但是當我放置代碼來實現接口時,VS2015不顯示給我實現類的選項,如果我沒有手動實現並編譯項目,它不會顯示編譯錯誤,不應該嗎?如果它顯示編譯錯誤告訴我,我沒有實現接口。沒有編譯錯誤的一個未實現的接口

接口:

public interface IRepository<T> where T : class 
{  
    IQueryable<T> GetAll(); 
    IQueryable<T> FindBy (Expression<Func<T, bool>> predicate); 
    void Add (T entity); 
    void Delete (T entity); 
    void Edit (T entity); 
    void Save(); 
} 

類應該實現IRepository,而不是工具,它不會拋出一個編譯錯誤:

public class GenericRepository<T> where T : class, IRepository<T> 
{ 
} 
+0

經過一番在谷歌搜索我發現的問題:它應該這樣寫:** public class GenericRepository :IRepository 其中TEntity:class' ** – c2s

+0

就像下面寫的答案? 'TEntity'或'T',沒關係。 – user3185569

回答

5

GenericRepository沒有實現IRepository。但它接受一個通用類型參數,該參數是實現IRepository的類型。

要強制GenericRepository實現IRepository你需要使用這樣的:

public class GenericRepository<T> : IRepository<T> where T : class 
{ 
    // implement the interface here 
} 

而如果你離開的代碼,因爲它是,你需要聲明你反對這樣的:

var repo = new GenericRepository<SomeClassThatImplementsIReporsitory>();