I cant remember where, but somebody gave me this peice of code to copy an instance of a class which does not have a clone method.
It works, but since i dont really understand it, i just want to know whether this is going to have any negative effects before i start using it more often... Also, will this function do a deep or shallow copy?
It works, but since i dont really understand it, i just want to know whether this is going to have any negative effects before i start using it more often... Also, will this function do a deep or shallow copy?
VB.NET:
Public Function CopyObject(ByVal obj As Object) As Object
'copies original object to stream then
'deserializes that stream and returns the output
'to create clone (copy) of object
Dim objMemStream As New MemoryStream(5000)
Dim objBinaryFormatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter(Nothing, _
New Runtime.Serialization.StreamingContext(Runtime.Serialization.StreamingContextStates.Clone))
objBinaryFormatter.Serialize(objMemStream, obj)
objMemStream.Seek(0, SeekOrigin.Begin)
CopyObject = objBinaryFormatter.Deserialize(objMemStream)
objMemStream.Close()
End Function
Last edited by a moderator: