自定义集合(属于T) [英] Custom Collection (Of T)
本文介绍了自定义集合(属于T)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建使用类型而不是 Object
的 Collection
的自定义版本.我有一个工作类(请参见下文),但是,我想将其更改为(T)
,而不必为我要使用的每种类型创建一个新类.可以实现吗?
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
推荐答案
您想将 CustomClass
更改为通用对象吗?
You want to change CustomClass
to be a generic object?
然后这很简单:
Public Class CustomCollection(Of T)
Implements IDictionary(Of String, T)
'etc...
End Class
这篇关于自定义集合(属于T)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文