Skip to main content
 首页 » 编程设计

c#之具有通用功能的接口(interface)

2024年02月13日14cmt

我有一个像这样的界面:

 public interface ITableData 
    { 
        List<T> ListAll<T>() where T : TableData; 
        void Insert(Object o); 
 
    } 

我的类实现了接口(interface):

  public class BookData:ITableData 
        { 
           public List<T> ListAll<T>() where T : TableData 
            { 
                //some code here 
            } 
        } 

事实上,我想要这样的结果:

public class BookData:ITableData 
{ 
  public List<Book> ListAll() 
  { List<Book> bookList =XXXXXX; 
//some code here 
return bookList} 
} 

如何实现? 谢谢大家。

请您参考如下方法:

将泛型参数移动到接口(interface)而不是方法上:

public interface ITableData<T> where T : TableData 
{ 
    List<T> ListAll(); 
    void Insert(Object o); 
} 
 
public class BookData : ITableData<Book> 
{ 
    public List<Book> ListAll() 
    { 
        List<Book> bookList =XXXXXX; 
        //some code here 
        return bookList; 
    } 
}