如何复制对象

时间:2014-08-18 11:31:11

标签: c# .net silverlight silverlight-5.0 xml-deserialization

我试图在 silverligth 5 中创建对象的副本,其中IFormatters和IcCloanble等接口不支持。 *

我的对象是这样的:(注意这些obkjects是在反序列化xml时获得的): 我试着像这样复制:

    [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);
            }
        }

    }

我想做一些事情就是这样:

objectOrginal.Clone();.

但是silverligth5中的问题是:

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?)

即使我这样做了。它仍然不起作用:

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;
        }

    }

Silverlight 5中是否有其他选择。请详细解释。 非常感谢。

在这种情况下,问题是:(即使我已经包含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?)    

0 个答案:

没有答案