使用XmlSerializer时如何忽略派生类中基类的属性?

发布于 2024-11-19 18:16:41 字数 1790 浏览 1 评论 0原文

我有一个这样的类:

[Serializable]
    public class Structure
    {
        #region Constants and Fields

        /// <summary>
        /// The description.
        /// </summary>
        private string description;


        #endregion


        /// <summary>
        ///   Gets or sets the Description of the subclass i.e subtype of structure
        /// </summary>
        public string Description
        {
            get
            {
                return this.description;
            }

            set
            {
                this.description = value;
            }
        }
}

下面的另一个类继承了上面的类:

[XmlRoot(Namespace = "TestNamespace", ElementName = "OrgStructure")]
    public class OrgStructure : Structure
    {


        private long orgDeptID;

        /// <summary>
        ///   The description
        /// </summary>
        private string description;

  public long OrgDeptID
        {
            get
            {
                return this.orgDeptID;
            }

            set
            {
                this.orgDeptID= value;

            }
        }
}

我正在将 ASMX 服务迁移到 WCF,使它们与现有的 ASMX 客户端兼容。所以我必须使用 XmlSerializer 而不是 DataContractSerializer

OrgStructureOperationContract 的响应类型中声明为 MessageBodyMember

ASMX 客户端期望 XML 消息中的描述。因此,我尝试隐藏(使用 new 运算符)派生类中的 Description 属性,并将 XmlIgnoreAttribute 应用于它。但它仍然序列化这个属性。

(请注意 description 变量的声明。我不知道为什么开发人员再次声明派生类,而不是将其protected保留在基类本身中。)

我怎样才能使用 XmlSerializer 时忽略派生类中基类的属性?我不能在基类中忽略它,因为 Structure 的其他子类型需要它。

I have a class like this:

[Serializable]
    public class Structure
    {
        #region Constants and Fields

        /// <summary>
        /// The description.
        /// </summary>
        private string description;


        #endregion


        /// <summary>
        ///   Gets or sets the Description of the subclass i.e subtype of structure
        /// </summary>
        public string Description
        {
            get
            {
                return this.description;
            }

            set
            {
                this.description = value;
            }
        }
}

Another class like below inherits above one:

[XmlRoot(Namespace = "TestNamespace", ElementName = "OrgStructure")]
    public class OrgStructure : Structure
    {


        private long orgDeptID;

        /// <summary>
        ///   The description
        /// </summary>
        private string description;

  public long OrgDeptID
        {
            get
            {
                return this.orgDeptID;
            }

            set
            {
                this.orgDeptID= value;

            }
        }
}

I am migrating ASMX service to WCF keeping them compatible with existing ASMX clients. So I have to use the XmlSerializer instead of DataContractSerializer.

The OrgStructure is declared as MessageBodyMember in the response type of an OperationContract.

The ASMX client does NOT expect the Description in the XML message. So I tried to hide (using new operator) the Description property in the derived class and applied XmlIgnoreAttribute to it. But still it serializes this property.

(Please note that the declaration of the description variable. I do not know why developer declared again the derived class instead keeping it protected in the base class itself.)

How can I ignore the property of the base class in the derived class while using the XmlSerializer? I cannot ignore it in the base class since other subtypes of Structure need it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉城メ夜风 2024-11-26 18:16:41

向基类添加:

public virtual bool ShouldSerializeDescription() { return true; }

向派生类添加:

public override bool ShouldSerializeDescription() { return false; }

这是 XmlSerializer 识别的模式,但必须在与成员相同的级别进行声明(Description ),因此需要将其设为虚拟的。

如果它冒犯了眼睛,请添加一些:

[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]

- 但它必须公共才能工作。

To the base class, add:

public virtual bool ShouldSerializeDescription() { return true; }

and to the derived class, add:

public override bool ShouldSerializeDescription() { return false; }

This is a pattern that XmlSerializer recognises, but must be declared at the same level as the member (Description), hence the need to make it virtual.

If it offends the eye, add some:

[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]

to it - but it must be public to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文