初始化
This commit is contained in:
37
BLV_Studio/Test/DeepCopyHelper.vb
Normal file
37
BLV_Studio/Test/DeepCopyHelper.vb
Normal file
@@ -0,0 +1,37 @@
|
||||
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
|
||||
Reference in New Issue
Block a user