Dim i = 2 Do While True i += 1 If IsDBNull(TmDataSet.T.Rows(0)(i)) = True Then Exit Do Dim new_t As New train new_t.id = TmDataSet.T.Rows(0)(i) Dim j = 0 Do Until IsDBNull(TmDataSet.T.Rows(j + 1)(i)) j += 1 Do Until (TmDataSet.T.Rows(j)(i) <> -1) j += 1 Loop If IsDBNull(TmDataSet.T.Rows(j + 1)(i)) Then Exit Do Dim new_st As New station new_st.t = TmDataSet.T.Rows(j)(i) new_st.name = TmDataSet.T.Rows(j)(1) new_st.id = TmDataSet.T.Rows(j)(2) new_st.id_t = new_st.id.ToString & new_st.t Dim new_st2 As New station Do Until (TmDataSet.T.Rows(j + 1)(i) <> -1) j += 1 Loop new_st2.t = TmDataSet.T.Rows(j + 1)(i) new_st2.name = TmDataSet.T.Rows(j + 1)(1) new_st2.id = TmDataSet.T.Rows(j + 1)(2) new_st2.id_t = new_st2.id.ToString & new_st2.t Dim list As New List(Of station) list.Add(new_st) list.Add(new_st2) new_t.st.Add(list) Loop per_network.Add(new_t) Loop
‘network = per_network的深层副本
vb>>>我只是想将per_network的内容复制到网络,我尝试过ToList方法,但它是浅拷贝而且无法执行克隆方法我根本没有得到它
您可以创建一个扩展方法,在该方法中序列化对象只是为了再次反序列化它.这将使用它自己的引用创建一个新对象,从而创建一个深层复制.
原文链接:/vb/255875.htmlPublic Module Extensions <System.Runtime.CompilerServices.Extension()> _ Public Function DeepCopy(Of T)(ByVal Obj As T) As T If Obj.GetType().IsSerializable = False Then Return Nothing Using MStream As New MemoryStream Dim Formatter As New BinaryFormatter Formatter.Serialize(MStream,Obj) MStream.Position = 0 Return DirectCast(Formatter.Deserialize(MStream),T) End Using End Function End Module
现在你可以打电话:
Dim network As List(Of train) = per_network.DeepCopy()
编辑:
这些是我上面代码的必需导入:
Imports System.IO Imports System.Runtime.Serialization.Formatters.Binary