Vb.net 定制收集(共套)
我正在尝试创建一个自定义版本的Vb.net 定制收集(共套),vb.net,collections,generic-collections,Vb.net,Collections,Generic Collections,我正在尝试创建一个自定义版本的集合,它使用类型而不是对象。我有一个工作类(见下文),但是,我想将其更改为(Of T),而不是为我想使用的每种类型创建一个新类。有可能实施这一点吗 Imports System.Collections.Generic Public Class CustomCollection Implements IDictionary(Of String, CustomClass) Private _dct As New Dictionary(Of String, Custo
集合
,它使用类型而不是对象
。我有一个工作类(见下文),但是,我想将其更改为(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
导入System.Collections.Generic
公共类自定义集合
实现IDictionary(字符串的,自定义类的)
私有dct作为新字典(字符串、自定义类)
#区域“词典成员”
Public Sub Add(ByVal键作为字符串,ByVal值作为CustomClass)实现Collections.Generic.IDictionary(字符串的,CustomClass的)。Add
Me.\u dct.Add(键、值)
端接头
公共函数ContainsKey(ByVal键作为字符串)作为布尔值实现System.Collections.Generic.IDictionary(字符串的,自定义类的)。ContainsKey
还给我。集装箱箱(钥匙)
端函数
公共只读属性键()作为ICollection(字符串的)实现System.Collections.Generic.IDictionary(字符串的,自定义类的)。键
得到
把钥匙还给我
结束
端属性
公共函数Remove(ByVal键作为字符串)作为布尔实现System.Collections.Generic.IDictionary(字符串的,CustomClass的)。Remove
返回我。\u dct.删除(键)
端函数
公共只读属性值()作为ICollection(属于CustomClass)实现System.Collections.Generic.IDictionary(属于字符串,属于CustomClass).Values
得到
返回我。\u dct.Values
结束
端属性
作为布尔值的公共函数TryGetValue(ByVal键作为字符串,ByRef值作为CustomClass)实现System.Collections.Generic.IDictionary(字符串的,CustomClass的)。TryGetValue
返回我。\u dct.TryGetValue(键,值)
端函数
作为CustomClass实现System.Collections.Generic.IDictionary的默认公共属性项(ByVal键作为字符串)。项
得到
作为CustomClass的Dim值=无
如果TryGetValue(键,值),则
返回值
如果结束
抛出新异常(“无效索引”)
结束
设置(ByVal值作为CustomClass)
Me.\u dct(键)=值
端集
端属性
#末端区域
#区域“ICollection成员”
Public Sub Add(作为KeyValuePair(字符串的,自定义类))的ByVal项实现System.Collections.Generic.ICollection(KeyValuePair(字符串的,自定义类)).Add
Dim d作为ICollection(KeyValuePair的(字符串的,自定义类))=TryCast(Me.\u dct,ICollection(KeyValuePair的(字符串的,自定义类)))
d、 添加(项目)
端接头
Public Sub Clear()实现System.Collections.Generic.ICollection(属于KeyValuePair(属于字符串,CustomClass)).Clear
我._dct.Clear()
端接头
公共函数包含(ByVal项作为KeyValuePair(字符串的,自定义类))作为布尔实现System.Collections.Generic.ICollection(KeyValuePair(字符串的,自定义类))。包含
Dim d作为ICollection(KeyValuePair的(字符串的,自定义类))=TryCast(Me.\u dct,ICollection(KeyValuePair的(字符串的,自定义类)))
返回d.Contains(项目)
端函数
公共子CopyTo(ByVal数组作为KeyValuePair(字符串的,CustomClass)(),ByVal arrayIndex作为整数)实现System.Collections.Generic.ICollection(KeyValuePair(字符串的,CustomClass)).CopyTo
Dim d作为ICollection(KeyValuePair的(字符串的,自定义类))=TryCast(Me.\u dct,ICollection(KeyValuePair的(字符串的,自定义类)))
d、 CopyTo(数组、数组索引)
端接头
作为整数的公共只读属性Count()实现System.Collections.Generic.ICollection(属于KeyValuePair(属于字符串,CustomClass)).Count
得到
还给我
结束
端属性
公共只读属性IsReadOnly()作为布尔值实现System.Collections.Generic.ICollection(属于KeyValuePair(属于字符串,CustomClass)).IsReadOnly
得到
返回错误
结束
端属性
公共函数Remove(ByVal项作为KeyValuePair(字符串的,自定义类))作为布尔实现System.Collections.Generic.ICollection(KeyValuePair(字符串的,自定义类))。Remove
Dim d作为ICollection(KeyValuePair的(字符串的,自定义类))=TryCast(Me.\u dct,ICollection(KeyValuePair的(字符串的,自定义类)))
返回d.删除(项目)
端函数
#末端区域
#区域“IEnumerable成员”
公共函数GetEnumerator2()作为IEnumerator(属于KeyValuePair(属于String,CustomClass))实现IEnumerable(属于KeyValuePair(属于String,CustomClass))。GetEnumerator
返回TryCast(Me.\u dct.GetEnumerator(),IEnumerator(属于KeyValuePair(属于字符串,属于CustomClass)))
端函数
#末端区域
#区域“IEnumerable成员”
私有函数GetEnumerator()作为System.Collections.IEnumerator实现System.Collections.IEnumerable.GetEnumerator
返回TryCast(Me.\u dct.GetEnumerator(),System.Collections.IEnumerator)
端函数
#末端区域
末级
是否要将CustomClass
更改为通用对象
这很简单:
Public Class CustomCollection(Of T)
Implements IDictionary(Of String, T)
'etc...
End Class
哈哈,这么简单。。。我以前从未真正使用过
(Of T)
,所以我尝试实现IDictionary(Of String,Of T)
lol。谢谢