1

我目前正在使用序列化来制作一些自动生成的 web 服务类的深层副本。是一种更清洁的方法吗?这些类是自动生成的,所以我不想碰它们。更多详情:

我当前的代码(工作正常)是这样的:

using (Stream stream = new MemoryStream()) //Copy IR
{
    IFormatter IF = new BinaryFormatter();
    IF.Serialize(stream, IR);
    stream.Seek(0, SeekOrigin.Begin);
    IR2 = (ObjectType)IF.Deserialize(stream);
}

该代码用于复制自动生成的类。当我向一个soap 请求添加一个webreference 时,Visual Studio 2005 会自动生成这个类等等。这些类看起来像这样:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.3074")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace=/*"[WebServiceNameSpace]"*/)]
public partial class ObjectType {

    private string AField;        
    private string BField;
    //...
    public string A {
        get {
            return this.AField;
        }
        set {
            this.AField = value;
        }
    }

    /// <remarks/>
    public string B {
        get {
            return this.BField;
        }
        set {
            this.BField = value;
        }
    }
    //...
}
4

1 回答 1

2

深度克隆是痛苦的。如果类支持序列化,那是最好的(=最可靠的)选项。其他任何事情都是很多工作。

实际上,由于类型支持 xml 序列化,所以这里的自然选择是XmlSerializer.

有你想解决的问题吗?表现?

于 2009-06-23T18:06:52.270 回答