這是.NET4中的Vb,但我在C#中遇到同樣的問題。 我們不得不使用接口的原因,我不會進入。不能在帶有接口的列表中使用AddRange
這工作,但它不使用接口:
Public class PersonList
Inherits List(of Person)
end class
Dim MyPeople as new PersonList
MyPeople.AddRange(SomeList)
我需要,因爲我們使用依賴注入,使這些類的測試使用,如IPerson和IPersonList接口。
當我爲PersonList創建一個接口時,我看不到AddRange。
PersonList的新版本使用的接口
Public class PersonList
Inherits List(IPerson)
Implements IList(Of IPerson)
Implements IPersonList
end class
Interface for the list:
Public Interface IPersonList
Inherits IList(Of IPerson)
end interface
寄託都建立和按預期工作,直到我打電話的AddRange:
Dim MyPeople as IPersonList = new PersonList
MyPeople.AddRange(SomeList) 'BUILD ERROR AddRange is not a member of IPersonList
想必IPersonList
需要從一些已經的實現繼承AddRange
。但AddRange
是List(of T)
的一部分,該接口不能從中繼承。
那麼如何讓我的列表界面暴露AddRange
?
循環'IList'不公開'AddRange',所以你需要將它自己添加。 – leppie 2013-03-01 14:33:12
不要繼承列表。 –
2013-03-01 14:48:44
在C#中,我認爲你可以通過在你的'IPersonList'接口中重新聲明'AddRange'來解決這個問題,然後從'List'繼承的'AddRange'將滿足它。但是因爲在VB中你必須有用於接口實現的'Implements'子句,所以我沒有辦法做到這一點。 – 2013-03-01 14:54:35