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