简体   繁体   English

Silverlight中的对象深层复制

[英]Object deep copy in Silverlight

I was trying to create copy of objects in silverligth 5 where interfaces like IFormatters and IcCloanble do not support. 我试图在silverligth 5中创建对象的副本,其中不支持IFormatters和IcCloanble之类的接口。 * *

My objects are like this: (Note that these obkjects are obtained on deserializing xml): I tried to do copy like this: 我的对象是这样的:(请注意,这些对象是在反序列化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 : 当然这行不通,然后我在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 : 但是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?)

is there any alternative in Silverlight 5 . Silverlight 5中有没有其他选择? Please explain in detail. 请详细说明。 Thanks a lot. 非常感谢。

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. 在您的类上实现DataContractSerializer属性(DataContract,DataMember),并调用DatacontractSerializer将其序列化为MemoryStream,然后再次使用它从MemoryStream序列化为该对象的新实例。 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 Microsoft文档http://msdn.microsoft.com/zh-cn/library/ms752244(v=vs.110).aspx中介绍了从一个类实例克隆到另一个类实例的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM