locked
Clone an Object! RRS feed

  • Question

  • Hello all!

    I'm developing in silverlight and I have a little problem.

    I have declaration of my object

    MyObject obj = new MyObject();

    obj.add(...)

    obj.add(...) 

    obj.add(...) 

     

    So, I need to clone my  obj to another object called obj2.

    how can I do?

    I can't write obj=obj2 because It would have the same reference but I don't want it.

     Thanks.

    Friday, January 22, 2010 5:49 AM

Answers

  •  you need to add a reference to System.Runtime.Serialization in Silverlight-app (located in:C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0)

     Then your code goes like this:

     

      public partial class MainPage : UserControl
      {
        public MainPage()
        {
          InitializeComponent();
    
          var p = new Person();
          var p2 = CopyPerson(p);
          if (p2.GetHashCode() != p.GetHashCode())
          {
            MessageBox.Show("Yes, that's a copy! :-)");
          }
        }
    
        private Person CopyPerson(Person p)
        {
          DataContractSerializer cs = new DataContractSerializer(typeof(Person));
          var ms = new MemoryStream();
          cs.WriteObject(ms, p);
          ms.Seek(0, SeekOrigin.Begin);
          return cs.ReadObject(ms) as Person;
        }
    
      }
      [DataContract]
      public class Person
      {
        [DataMember]
        public string Name { get; set; }
      }
      
    Friday, January 22, 2010 8:25 AM
  •  Hi Magozeta,

    I can place the class also in a library. You need to add the reference i mentioned.

    Friday, January 22, 2010 9:11 AM

All replies

  •  You can use the DataContractSerializer of WCF for that. Don't forget to place DataMember-Attributes on your object's class.

    Friday, January 22, 2010 6:42 AM
  •  thanks for reply but....I have my class MyObject in project silverlight and not in project web.

    can you explain me how can I do? thanks

    Friday, January 22, 2010 8:06 AM
  •  you need to add a reference to System.Runtime.Serialization in Silverlight-app (located in:C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0)

     Then your code goes like this:

     

      public partial class MainPage : UserControl
      {
        public MainPage()
        {
          InitializeComponent();
    
          var p = new Person();
          var p2 = CopyPerson(p);
          if (p2.GetHashCode() != p.GetHashCode())
          {
            MessageBox.Show("Yes, that's a copy! :-)");
          }
        }
    
        private Person CopyPerson(Person p)
        {
          DataContractSerializer cs = new DataContractSerializer(typeof(Person));
          var ms = new MemoryStream();
          cs.WriteObject(ms, p);
          ms.Seek(0, SeekOrigin.Begin);
          return cs.ReadObject(ms) as Person;
        }
    
      }
      [DataContract]
      public class Person
      {
        [DataMember]
        public string Name { get; set; }
      }
      
    Friday, January 22, 2010 8:25 AM
  • Hi

    I guess you have to do it by yourselves

    MyObject obj2 = new MyObject();

    foreach(var o in obj)
            obj2.add(new x{...............})  /// set all props from o to the new x

    Ofcorse this could be an method of MyObject e.g Clone()

    Friday, January 22, 2010 8:26 AM
  • -to thomas thomas: ok thanks. but my class "Person" doesnt accept [DataContract]and [Datamember] because, i think, is in silverlight library class.

     

    -to swo: I have an object that contains many other object....I would create a standard method .

     

    how can I do ?

    Friday, January 22, 2010 8:58 AM
  •  Hi Magozeta,

    I can place the class also in a library. You need to add the reference i mentioned.

    Friday, January 22, 2010 9:11 AM
  •  ok thanks!

    Friday, January 22, 2010 10:49 AM
  •  Can I do this method a  generic Method?

    a generic method which accept in input an Object?

    thanks!

    Wednesday, March 10, 2010 9:33 AM
  •     public static T Clone<T>(T source)
            {
                DataContractSerializer serializer = new DataContractSerializer(typeof(T));
                using (MemoryStream ms = new MemoryStream())
                {
                    serializer.WriteObject(ms, source);
                    ms.Seek(0, SeekOrigin.Begin);
                    return (T)serializer.ReadObject(ms);
                }
            }

    Thursday, April 15, 2010 7:06 AM