Files
Desktop_BLVStudio_EN/BLV_Studio/Test/DeepCopyHelper.vb
2025-12-11 14:22:51 +08:00

38 lines
1.0 KiB
VB.net

Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Imports Processform.DeviceModel
Public Class DeepCopyHelper
Public Shared Function DeepCopy(Of T)(ByVal obj As T) As T
If Not GetType(T).IsSerializable Then
Throw New ArgumentException("The type must be serializable.", NameOf(obj))
End If
If obj Is Nothing Then
Return Nothing
End If
Dim formatter As BinaryFormatter = New BinaryFormatter()
Using stream As MemoryStream = New MemoryStream()
formatter.Serialize(stream, obj)
stream.Seek(0, SeekOrigin.Begin)
Return CType(formatter.Deserialize(stream), T)
End Using
End Function
Public Shared Function DictionaryCopy(dic As Dictionary(Of String, DeviceModel)) As Dictionary(Of String, DeviceModel)
Dim resultdic As New Dictionary(Of String, DeviceModel)
For Each index In dic
resultdic.Add(index.Key, index.Value)
Next
Return resultdic
End Function
End Class