Click here to Skip to main content
15,891,805 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All
performance problem while using CreateDeepCopy method in webservice.
Can any one suggest me better method which is similar to createdeepcopy
Thanks in advance
Karthick
Posted
Comments
Yuriy Loginov 30-May-13 21:15pm    
Deep copy is usually an expensive operation as you will create new instances of objects. Do you really need a deep copy? You can use clone method to get the shallow copy.

1 solution

Creating a deep copy sounds familiar to me, please try this example:
C#
public static T CloneObject<T>(T item)
{
  if (item != null)
  {
    using (MemoryStream ms = new MemoryStream())
    {
      BinaryFormatter bf = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Clone));
      bf.Serialize(ms, item);
      ms.Seek(0, SeekOrigin.Begin);
      return (T)bf.Deserialize(ms);
    }
  }
  return default(T);
}

Of course, You have to include some namespaces:
C#
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

Enjoy :)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900