对于原始类型字典
Public Sub runIntDictionary()
Dim myIntegerDict As New Dictionary(Of Integer, Integer) From {{0, 0}, {1, 1}, {2, 2}}
Dim cloneIntegerDict As New Dictionary(Of Integer, Integer)
cloneIntegerDict = myIntegerDict.Select(Function(x) x.Key).ToList().ToDictionary(Of Integer, Integer)(Function(x) x, Function(y) myIntegerDict(y))
End Sub
对于实现 ICloneable 的 Object 的字典
Public Sub runObjectDictionary()
Dim myDict As New Dictionary(Of Integer, number) From {{3, New number(3)}, {4, New number(4)}, {5, New number(5)}}
Dim cloneDict As New Dictionary(Of Integer, number)
cloneDict = myDict.Select(Function(x) x.Key).ToList().ToDictionary(Of Integer, number)(Function(x) x, Function(y) myDict(y).Clone)
End Sub
Public Class number
Implements ICloneable
Sub New()
End Sub
Sub New(ByVal newNumber As Integer)
nr = newnumber
End Sub
Public nr As Integer
Public Function Clone() As Object Implements ICloneable.Clone
Return New number With {.nr = nr}
End Function
Public Overrides Function ToString() As String
Return nr.ToString
End Function
End Class