深层复制List(Of Class)中的元素

时间:2015-03-26 21:25:46

标签: vb.net list class clone deep-copy

晚上好。

我试图对列表中的某些元素进行深层复制,而且我发现它很难做到。如果下面的一些代码看起来很奇怪,我会尝试很多新的概念,所以请耐心等待......

我有以下列表:

accountList as List(Of Account)
accountsInArea as New List(Of Account)
result as New List(Of Account)

每个元素的类型都是我自己定义的类:

Public Class Account

Enum AddingType
    income
    expenditure
End Enum
Enum AreaType
    high
    mid
    low
End Enum

Property Area() As AreaType
Property Group() As String
Property AccountNumber() As Integer
Property AccountDescription() As String
Property IncomeExpenditure() As AddingType
Public Property MonthlyValues As New Dictionary(Of Integer, Decimal)
End Class

我需要找到accountList某些项,并将复制到新列表中:accountsInArea。目前,我使用了以下方法:

accountsInArea = accountList.FindAll(Function(acc) acc.Area = area)

然而,这会产生引用的副本到Lambda Expression产生的那些项目(不是吗?)。如果我在浏览SO和MSDN时正确理解,我需要的是深层复制元素。

我希望accountsInArea拥有所有.FindAll项生成的副本,包括(特别是)MonthlyValues字典中的项目。

我试过了:

accountsInArea = accountList.Select(accountList.FindAll(Function(acc) acc.Area = area))

此外:

For Each account In accountList.FindAll(Function(acc) acc.Area = area)
    accountsInArea.Add(account)
Next

最后加入班级:

Implements ICloneable

Public Function Clone() As Object Implements System.ICloneable.Clone
    Return Me.MemberwiseClone
End Function

在方法内部使用:

accountsInArea = accountList.FindAll(Function(acc) acc.Area = area)
    result = accountsInArea.Select(Function(acc) acc.Clone()).Cast(Of Account).ToList

非常感谢帮助。

2 个答案:

答案 0 :(得分:0)

经过多一点修补,我发现了一个解决方案。虽然我确信有更优雅的东西,但我会在这里发布,以防有​​人急需:

我最终在方法中使用了以下代码:

Dim accountsInArea as New List(Of Account)

For Each account In accountList.FindAll(Function(acc) acc.Area = area)
    Dim a As New Account
    With a
        .Area = account.Area
        .AccountDescription = account.AccountDescription
        .Group = account.Group
        .IncomeExpenditure = account.IncomeExpenditure
        .AccountNumber = account.AccountNumber
        .MonthlyValues = New Dictionary(Of Integer, Decimal)
    End With
    For Each val In account.MonthlyValues
        Dim k As Integer = v.Key
        Dim v As Decimal = v.Value
        a.MonthlyValues.Add(k, v)
    Next
    accountsInArea.Add(a)
Next

因此,如果有人知道一种不太复杂的方式(可能涉及我在问题中提到的方法之一),那将是非常棒的。

非常感谢。

答案 1 :(得分:0)

只需在您的类中实现ICloneable,并且

Dim accountsInArea As List(Of Account) = accountList.Where(Function(x) x.Area = Account.AreaType.high).Select(Function(y) y.clone).Cast(Of Account)().ToList

此处为您前任的完整代码

Sub AccountListProg
    Dim accountList As New List(Of Account) From {New Account With {.Area = Account.AreaType.high, .AccountNumber = 1}, _
                                                   New Account With {.Area = Account.AreaType.high, .AccountNumber = 2}, _
                                                   New Account With {.Area = Account.AreaType.low, .AccountNumber = 3}, _
                                                   New Account With {.Area = Account.AreaType.mid, .AccountNumber = 4}}

    Dim accountsInArea As List(Of Account) = accountList.Where(Function(x) x.Area = Account.AreaType.high).Select(Function(y) y.clone).Cast(Of Account)().ToList
End Sub

Public Class Account
    Implements ICloneable
    Public Function Clone() As Object Implements ICloneable.Clone
        Return New Account With {.AccountDescription = AccountDescription, _
                             .AccountNumber = AccountNumber, _
                             .Area = Area, _
                             .Group = Group, _
                             .IncomeExpenditure = AddingType.income, _
                             .MonthlyValues = MonthlyValues}
    End Function
    Enum AddingType
        income
        expenditure
    End Enum
    Enum AreaType
        high
        mid
        low
    End Enum

    Property Area() As AreaType
    Property Group() As String
    Property AccountNumber() As Integer
    Property AccountDescription() As String
    Property IncomeExpenditure() As AddingType
    Public Property MonthlyValues As New Dictionary(Of Integer, Decimal)
End Class