深度复制列表中的元素(Class)

Deep copying of elements in a List (Of Class)

晚上好。

我正在尝试对列表的某些元素进行深度复制,但我发现这很难做到。我正在尝试很多新概念,如果以下某些代码看起来很奇怪,请多多包涵...

我有以下列表:

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

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

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 表达式生成的那些项目生成 reference 的副本(不是吗?)。如果我在浏览 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

最后添加到 Class:

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

非常感谢您的帮助。

经过一番修修补补,我找到了解决办法。虽然我确信还有更优雅的东西,但我会 post 在这里以防有人急需:

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

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

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

谢谢。

只需在您的 Class 中实现 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