Custom Collection (Of T)

依然范特西╮ 提交于 2019-12-13 02:57:24

问题


I'm trying to create a custom version of Collection that uses a type instead of Object. I have a working class (see below), however, I would like to change it to be (Of T) instead of having to create a new class for each type that I would like to use. Is it possible to implement this?

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:


You want to change CustomClass to be a generic object?

Then this is quite simple:

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

    'etc...

End Class


来源:https://stackoverflow.com/questions/20216583/custom-collection-of-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!