2013-11-26 3 views
0

Я пытаюсь создать пользовательскую версию Collection, которая использует тип вместо Object. У меня есть рабочий класс (см. Ниже), однако я хотел бы изменить его как (Of T) вместо того, чтобы создавать новый класс для каждого типа, который я хотел бы использовать. Можно ли это реализовать?Custom Collection (Of 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 (Of String, Of T) 'lol. благодаря – Grahamvs