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

问题描述 投票:0回答:2

晚上好。

我正在尝试对列表中的某些元素进行深层复制,而我发现这样做非常困难。我正在尝试许多新概念,如果以下某些代码看起来很奇怪,请耐心等待...

我有以下列表:

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表达式生成的那些项的reference的副本(不是吗?)。如果在SO和MSDN上浏览时我理解正确,那么我需要的是元素的deep copy

我想让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

非常感谢您的帮助。

vb.net list class clone deep-copy
2个回答
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

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

仍然感谢。


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
© www.soinside.com 2019 - 2024. All rights reserved.