是否可以在.net中實現具有2個接口的類型說明符?例如:在.net中指定實現2個接口的變量
Public Sub New(obj as ICollection and INotifyCollectionChanged)
''Initialize Code
End Sub
是否可以在.net中實現具有2個接口的類型說明符?例如:在.net中指定實現2個接口的變量
Public Sub New(obj as ICollection and INotifyCollectionChanged)
''Initialize Code
End Sub
您可以使用泛型約束;例如在C#中:
public void Something<T>(T obj)
where T : IFoo, IBar
{....}
然後Something(value)
將只在value
的類型是東西,同時實現了IFoo
和IBar
。
不,您將不得不定義一個繼承自所需接口的接口。
Public Interface IMyCombinedInterface
Inherits IColllection, INotifyCollectionChanged
End Interface
謝謝喬恩。如果我實現IMyCombinedInterface,我不能將標準的ObservableCollection發送給構造函數,因爲它沒有實現IMyCombindeInterface。是語言的限制,還是我錯過了什麼? – PeterM 2011-03-13 06:26:51
感謝馬克,這正是我需要的。在Vb.net中,您可以在花括號中添加多個約束。見http://geekswithblogs.net/TechnicalDebtor/archive/2008/11/18/vb.net-generics-with-multiple-constraints.aspx – PeterM 2011-03-13 20:56:20
顯然只有在定義一個Generic類型的時候。不是在嘗試傳遞給函數時。即你不能這樣做: '公共函數Foo(x作爲{iBar,iBaz})作爲布爾' – 2012-09-24 21:40:05