Provides a deep-copy, field-level duplication of any object
Syntax
Visual Basic (Declaration) | |
---|
Public Class MemberwiseClone
Inherits ObjectCloner |
Example
Library/Library.Test/TestCloning.cs
C# | Copy Code |
---|
const int Reps = 1000;
TestObject test = TestObject.Create();
object result;
Stopwatch timer = new Stopwatch();
timer.Start();
for (int i = 0; i < Reps; i++)
{
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream m = new MemoryStream())
{
bf.Serialize(m, test);
m.Position = 0;
result = bf.Deserialize(m);
}
}
timer.Stop();
Console.WriteLine("Elapsed: {0,10} (100%)", timer.ElapsedTicks);
long percentof = timer.ElapsedTicks;
timer = new Stopwatch();
timer.Start();
for (int i = 0; i < Reps; i++)
{
result = new SerializerClone().Clone(test);
}
timer.Stop();
Console.WriteLine("Elapsed: {0,10} ({1}%)", timer.ElapsedTicks, (timer.ElapsedTicks * 100) / percentof);
timer = new Stopwatch();
timer.Start();
for (int i = 0; i < Reps; i++)
{
result = new MemberwiseClone().Clone(test);
}
timer.Stop();
Console.WriteLine("Elapsed: {0,10} ({1}%)", timer.ElapsedTicks, (timer.ElapsedTicks * 100) / percentof); |
VB.NET | Copy Code |
---|
Const Reps As Integer = 1000
Dim test As TestObject = TestObject.Create()
Dim result As Object
Dim timer As New Stopwatch()
timer.Start()
Dim i As Integer = 0
While i < Reps
Dim bf As New BinaryFormatter()
Using m As New MemoryStream()
bf.Serialize(m, test)
m.Position = 0
result = bf.Deserialize(m)
End Using
System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
End While
timer.[Stop]()
Console.WriteLine("Elapsed: {0,10} (100%)", timer.ElapsedTicks)
Dim percentof As Long = timer.ElapsedTicks
timer = New Stopwatch()
timer.Start()
Dim i As Integer = 0
While i < Reps
result = New SerializerClone().Clone(test)
System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
End While
timer.[Stop]()
Console.WriteLine("Elapsed: {0,10} ({1}%)", timer.ElapsedTicks, (timer.ElapsedTicks * 100) / percentof)
timer = New Stopwatch()
timer.Start()
Dim i As Integer = 0
While i < Reps
result = New MemberwiseClone().Clone(test)
System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
End While
timer.[Stop]()
Console.WriteLine("Elapsed: {0,10} ({1}%)", timer.ElapsedTicks, (timer.ElapsedTicks * 100) / percentof) |
Inheritance Hierarchy
Requirements
Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7
See Also