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?)
Even if i do something like this.It still do not work :
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System;
using System.IO;
using System.Runtime.Serialization;
public static class ObjectCopier
{
public static Attributes DeepCopy(this Attributes oSource)
{
Attributes oClone;
DataContractSerializer dcs = new DataContractSerializer(typeof(T));
using (MemoryStream ms = new MemoryStream())
{
dcs.WriteObject(ms, oSource);
ms.Position = 0;
oClone = (Attributes)dcs.ReadObject(ms);
}
return oClone;
}
}
is there any alternative in Silverlight 5 . Please explain in detail. Thanks a lot.
In this case the problem is : (even i have included System.Runtime.Serialization
)
Error 5 The type or namespace name 'DataContractSerializer' could not be found (are you missing a using directive or an assembly reference?)
Error 6 The type or namespace name 'DataContractSerializer' could not be found (are you missing a using directive or an assembly reference?)