2013-11-26 80 views
0

我試圖創建一個自定義版本Collection使用類型而不是Object。我有一個工作班(見下文),但是,我想將其更改爲(Of T),而不必爲每個我想要使用的類型創建一個新班級。有沒有可能實現這一點?自定義集合(的T)

Imports System.Collections.Generic 

Public Class CustomCollection 
Implements IDictionary(Of String, CustomClass) 

Private _dct As New Dictionary(Of String, CustomClass) 

#Region " IDictionary<string,object> Members " 

Public Sub Add(ByVal key As String, ByVal value As CustomClass) Implements Collections.Generic.IDictionary(Of String, CustomClass).Add 
    Me._dct.Add(key, value) 
End Sub 

Public Function ContainsKey(ByVal key As String) As Boolean Implements System.Collections.Generic.IDictionary(Of String, CustomClass).ContainsKey 
    Return Me._dct.ContainsKey(key) 
End Function 

Public ReadOnly Property Keys() As ICollection(Of String) Implements System.Collections.Generic.IDictionary(Of String, CustomClass).Keys 
    Get 
     Return Me._dct.Keys 
    End Get 
End Property 

Public Function Remove(ByVal key As String) As Boolean Implements System.Collections.Generic.IDictionary(Of String, CustomClass).Remove 
    Return Me._dct.Remove(key) 
End Function 

Public ReadOnly Property Values() As ICollection(Of CustomClass) Implements System.Collections.Generic.IDictionary(Of String, CustomClass).Values 
    Get 
     Return Me._dct.Values 
    End Get 
End Property 

Public Function TryGetValue(ByVal key As String, ByRef value As CustomClass) As Boolean Implements System.Collections.Generic.IDictionary(Of String, CustomClass).TryGetValue 
    Return Me._dct.TryGetValue(key, value) 
End Function 

Default Public Property Item(ByVal key As String) As CustomClass Implements System.Collections.Generic.IDictionary(Of String, CustomClass).Item 
    Get 
     Dim value As CustomClass = Nothing 
     If TryGetValue(key, value) Then 
      Return value 
     End If 
     Throw New Exception("invalid index") 
    End Get 
    Set(ByVal value As CustomClass) 
     Me._dct(key) = value 
    End Set 
End Property 

#End Region 

#Region " ICollection<KeyValuePair<string,object>> Members " 

Public Sub Add(ByVal item As KeyValuePair(Of String, CustomClass)) Implements System.Collections.Generic.ICollection(Of KeyValuePair(Of String, CustomClass)).Add 
    Dim d As ICollection(Of KeyValuePair(Of String, CustomClass)) = TryCast(Me._dct, ICollection(Of KeyValuePair(Of String, CustomClass))) 
    d.Add(item) 
End Sub 

Public Sub Clear() Implements System.Collections.Generic.ICollection(Of KeyValuePair(Of String, CustomClass)).Clear 
    Me._dct.Clear() 
End Sub 

Public Function Contains(ByVal item As KeyValuePair(Of String, CustomClass)) As Boolean Implements System.Collections.Generic.ICollection(Of KeyValuePair(Of String, CustomClass)).Contains 
    Dim d As ICollection(Of KeyValuePair(Of String, CustomClass)) = TryCast(Me._dct, ICollection(Of KeyValuePair(Of String, CustomClass))) 
    Return d.Contains(item) 
End Function 

Public Sub CopyTo(ByVal array As KeyValuePair(Of String, CustomClass)(), ByVal arrayIndex As Integer) Implements System.Collections.Generic.ICollection(Of KeyValuePair(Of String, CustomClass)).CopyTo 
    Dim d As ICollection(Of KeyValuePair(Of String, CustomClass)) = TryCast(Me._dct, ICollection(Of KeyValuePair(Of String, CustomClass))) 
    d.CopyTo(array, arrayIndex) 
End Sub 

Public ReadOnly Property Count() As Integer Implements System.Collections.Generic.ICollection(Of KeyValuePair(Of String, CustomClass)).Count 
    Get 
     Return Me._dct.Count 
    End Get 
End Property 

Public ReadOnly Property IsReadOnly() As Boolean Implements System.Collections.Generic.ICollection(Of KeyValuePair(Of String, CustomClass)).IsReadOnly 
    Get 
     Return False 
    End Get 
End Property 

Public Function Remove(ByVal item As KeyValuePair(Of String, CustomClass)) As Boolean Implements System.Collections.Generic.ICollection(Of KeyValuePair(Of String, CustomClass)).Remove 
    Dim d As ICollection(Of KeyValuePair(Of String, CustomClass)) = TryCast(Me._dct, ICollection(Of KeyValuePair(Of String, CustomClass))) 
    Return d.Remove(item) 
End Function 

#End Region 

#Region " IEnumerable<KeyValuePair<string,object>> Members " 

Public Function GetEnumerator2() As IEnumerator(Of KeyValuePair(Of String, CustomClass)) Implements IEnumerable(Of KeyValuePair(Of String, CustomClass)).GetEnumerator 
    Return TryCast(Me._dct.GetEnumerator(), IEnumerator(Of KeyValuePair(Of String, CustomClass))) 
End Function 

#End Region 

#Region " IEnumerable Members " 

Private Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator 
    Return TryCast(Me._dct.GetEnumerator(), System.Collections.IEnumerator) 
End Function 

#End Region 

End Class 

回答

1

你想更改CustomClass爲通用對象嗎?

那麼這是很簡單的:

Public Class CustomCollection(Of T) 
    Implements IDictionary(Of String, T) 

    'etc... 

End Class 
+0

哈哈,就這麼簡單......我從來沒有真正使用'(Of T)已'過,所以我試圖'器具的IDictionary(字符串,效應的T )'大聲笑。謝謝 – Grahamvs