CloneObject – cloning an object by serializing it to a memory stream

CloneObject – cloning an object by serializing it to a memory stream

' A reusable function that does object cloning' by serializing it to a memory streamFunction CloneObject(ByVal obj As Object) As Object    ' Create a memory stream and a formatter.    Dim ms As New System.IO.MemoryStream(1000)    Dim bf As New BinaryFormatter(Nothing, _        New StreamingContext(StreamingContextStates.Clone))    ' Serialize the object into the stream.    bf.Serialize(ms, obj)    ' Position streem pointer back to first byte.    ms.Seek(0, SeekOrigin.Begin)    ' Deserialize into another object.    CloneObject = bf.Deserialize(ms)    ' release memory.    ms.Close()End Function' this procedure tests object cloningSub TestCloneObject()    ' create an object graph    Dim p1 As New Person("Joe", "Doe", #1/2/1960#)    Dim p2 As New Person("Ann", "Smith", #3/4/1965#)    p1.Spouse = p2    p2.Spouse = p1    ' Clone it    Dim q1 As Person = DirectCast(CloneObject(p1), Person)    Dim q2 As Person = q1.Spouse    ' Prove that properties were copied correctly.    Console.WriteLine(q1.FirstName & " " & q1.LastName)    Console.WriteLine(q2.FirstName & " " & q2.LastName)    Console.WriteLine("P1 is Q1 = {0}", p1 Is q1)    Console.WriteLine("P2 is Q2 = {0}", p2 Is q2)End Sub' Note: This code is taken from Francesco Balena's' "Programming Microsoft Visual Basic .NET" - MS Press 2002, ISBN 0735613753' You can read a free chapter of the book at '

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes