【问题标题】:Cleaner way to deep copy an AutoGenerated WebService Class深度复制自动生成的 WebService 类的更简洁方法
【发布时间】:2010-11-05 06:26:50
【问题描述】:

我目前正在使用序列化来制作一些自动生成的 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;
        }
    }
    //...
}

【问题讨论】:

    标签: c# web-services serialization clone


    【解决方案1】:

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

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

    有你想解决的问题吗?性能?

    【讨论】:

    • 不,我要解决的问题没有。只是想看看是否有更清洁的方法来做到这一点。我并不特别惊讶没有,但我想我会问。 (我意识到将序列化代码移动到单独的通用函数或其他任何东西很容易,但这并没有使任何东西更干净。)
    猜你喜欢
    • 2017-12-25
    • 2023-03-18
    • 2015-12-02
    • 1970-01-01
    • 2014-07-05
    • 1970-01-01
    • 2018-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多