2

I was trying to create copy of objects in silverligth 5 where interfaces like IFormatters and IcCloanble do not support. *

My objects are like this: (Note that these obkjects are obtained on deserializing xml): I tried to do copy like this:

    [XmlRoot(ElementName = "component")]
        public class Component
        {
            [XmlElement("attributes")]
            public Attributes Attributes { get; set; } 

            [XmlIgnore]
            public Attributes atrbtOrginal = new Attributes();
            [XmlIgnore]
            public Attributes atrbtCopy{ get; set; }
        }
        public Component()
            {          
                atrbtCopy= atrbtOrginal ;
            } 

Sure it will not work then i got this code on seraching on Google :

 public static class ObjectCopier
    {
        public static T Clone<T>(T source)
        {
            if (!typeof(T).IsSerializable)
            {
                throw new ArgumentException("The type must be serializable.", "source");
            }

            // Don't serialize a null object, simply return the default for that object
            if (Object.ReferenceEquals(source, null))
            {
                return default(T);
            }
            IFormatter formatter = new BinaryFormatter();
            Stream stream = new MemoryStream();
            using (stream)
            {
                formatter.Serialize(stream, source);
                stream.Seek(0, SeekOrigin.Begin);
                return (T)formatter.Deserialize(stream);
            }
        }

    }

And i thought of doing something liek this:

objectOrginal.Clone();.

But the problem in silverligth5 is :

Error   2   The type or namespace name 'BinaryFormatter' could not be found (are you missing a using directive or an assembly reference?)   
Error   1   The type or namespace name 'IFormatter' could not be found (are you missing a using directive or an assembly reference?)

is there any alternative in Silverlight 5 . Please explain in detail. Thanks a lot.

Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53
user3735822
  • 337
  • 2
  • 12

1 Answers1

1

Implement the DataContractSerializer attributes (DataContract, DataMember) on your classes and call DatacontractSerializer to serialize it to a MemoryStream, then use it again to serialize out of the MemoryStream to a new instance of the object. By far the easiest to understand, and quite performant too.

Example of class definition :

[DataContract]
class MyClass
{
    [DataMember]
    public int MyValue {get;set;}
    [DataMember]
    public string MyOtherValue {get;set;}
}

The method of cloning from one class instance to another is covered in the Microsoft documentation http://msdn.microsoft.com/en-us/library/ms752244(v=vs.110).aspx

PhillipH
  • 6,182
  • 1
  • 15
  • 25
  • 1
    If he has access to the source code of the classes, why wouldn't he just implement a `Copy` method or have a copy constructor? – SimpleVar Aug 18 '14 at 09:15
  • @PhillipH .. Thanks but could you please elaborate more. I am not able to understand yet. Actually these attribute class is obtained on deserialisation of xml (i wanted to add for your information). Thanks a lot – user3735822 Aug 18 '14 at 09:16
  • They could; but the DCS method requires no logic - you dont need to know how the copy works and it reuses a high quality pre-existing framework. – PhillipH Aug 18 '14 at 09:19
  • @user3735822 - I've updated the reply to include the links to the microsoft article that contains the sample code to make this work. – PhillipH Aug 18 '14 at 09:25
  • @PhillipH please see the edit of question i have added my code. I already have deserialized xml in to class objects and those obtained objects i have to copy to another object. And i had used "using System.Xml.Serialization;" for it. How to do the object copy in this case now ? – user3735822 Aug 18 '14 at 09:42
  • @user3735822 Using XMLSerializable is like DatacontractSerializer, but DataContractSerializer is available in Silverlight - so use that instead. Edit the classes and use DataContract and DataMember attributes (you can use them in addition to your existing Xml attributes). I think the Microsoft link I posted gives a good example of how to achieve this. – PhillipH Aug 18 '14 at 12:36