使用XmlSerializer时如何忽略派生类中基类的属性?
我有一个这样的类:
[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
。
OrgStructure
在 OperationContract
的响应类型中声明为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
向基类添加:
向派生类添加:
这是
XmlSerializer
识别的模式,但必须在与成员相同的级别进行声明(Description
),因此需要将其设为虚拟的。如果它冒犯了眼睛,请添加一些:
- 但它必须是
公共
才能工作。To the base class, add:
and to the derived class, add:
This is a pattern that
XmlSerializer
recognises, but must be declared at the same level as the member (Description
), hence the need to make itvirtual
.If it offends the eye, add some:
to it - but it must be
public
to work.