【问题标题】:Serializing a referenced class Object序列化引用的类对象
【发布时间】:2012-04-17 16:40:00
【问题描述】:
// Reference.cs
// Conveintly serializable
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.225")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:schemas-sc-jp:mfp:osa-1-1")]
public partial class CREDENTIALS_TYPE : CREDENTIALS_BASE_TYPE {

    private string datatypeField;

    private OPAQUE_DATA_TYPE metadataField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("data-type")]
    public string datatype {
        get {
            return this.datatypeField;
        }
        set {
            this.datatypeField = value;
        }
    }

    /// <remarks/>
    public OPAQUE_DATA_TYPE metadata {
        get {
            return this.metadataField;
        }
        set {
            this.metadataField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.225")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:schemas-sc-jp:mfp:osa-1-1")]
public partial class OPAQUE_DATA_TYPE {

    private System.Xml.XmlElement[] anyField;

    private System.Xml.XmlAttribute[] anyAttrField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAnyElementAttribute()]
    public System.Xml.XmlElement[] Any {
        get {
            return this.anyField;
        }
        set {
            this.anyField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAnyAttributeAttribute()]
    public System.Xml.XmlAttribute[] AnyAttr {
        get {
            return this.anyAttrField;
        }
        set {
            this.anyAttrField = value;
        }
    }
}

我有一个从网络上的设备获取数据的网络方法,其中一部分数据是用于授权设备的凭据。

我们正在使用 API,因此要接收此数据,我必须将 CREDENTIALS_TYPE 作为 web 方法中的参数类型。我需要做的是保存这些数据,这样就可以在没有 web 方法的情况下获取它。

现在我之前已经将类序列化为 XML 文件,所以我选择了熟悉的选项,但只使用我自己创建的类。 我想知道是否真的可以对不属于我的引用类做同样的事情?

我必须创建一个与 CREDENTIALS_TYPE 匹配的类并复制数据,但我无法找到在不同类型之间进行转换的方法。

“CREDENTAILS_TYPE”来自 MFP,因此在我的情况下反映和更改代码并不好,因为无论如何我都无法更新它。

有什么建议/建议吗?

【问题讨论】:

  • 这门课是不是“你的”有什么关系呢? “我的”甚至是什么意思
  • 我的,因为我可以访问源代码并且可以更改和重新编译它们。
  • 我的问题是,为什么你认为你必须修改它?
  • 我试了也没用。我已经 ahem 自从修复它,它不需要修改 >_>
  • 你是怎么解决的?如果您的解决方案对其他人有帮助,您应该将其添加为答案,我会投赞成票。

标签: c# .net serialization xml-serialization asmx


【解决方案1】:

由于您无法修改不属于您的源代码的类,请使用您需要的序列化属性创建您自己的 DTO,然后使用Automapper 在两者之间来回传输字段数据。

【讨论】:

  • 谢谢你,Automapper 看起来可能正是我想要的。 :)
  • 接受,因为这是我正在寻找的,我只是不需要它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-15
  • 1970-01-01
  • 2015-08-30
  • 1970-01-01
  • 1970-01-01
  • 2018-01-21
  • 2010-10-13
相关资源
最近更新 更多