XmlSerializer ShouldSerialize* 不适用于基本类型属性
如果我为当前类型属性定义 ShouldSerialize* ,它就可以工作。但如果我指定基本类型属性,它就不起作用。 *指定也同样
[XmlInclude(typeof(SingleEventGroup))]
[XmlInclude(typeof(MultipleEventsGroup))]
public abstract class EventsGroup
{
public List<int> EventsIds { get; set; }
public string Name { get; set; }
}
public class SingleEventGroup : EventsGroup
{
public bool ShouldSerializeName()
{
return false; //it is still serialized
}
}
If I define ShouldSerialize* for current type property, it works. But it doesn't work if I specify base type property. The same for *Specified
[XmlInclude(typeof(SingleEventGroup))]
[XmlInclude(typeof(MultipleEventsGroup))]
public abstract class EventsGroup
{
public List<int> EventsIds { get; set; }
public string Name { get; set; }
}
public class SingleEventGroup : EventsGroup
{
public bool ShouldSerializeName()
{
return false; //it is still serialized
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
XmlSerializer 在成员
Name
的 .DeclaringType 上查找方法,而不是在 .ReflectedType 上查找。这就是为什么它不起作用。The XmlSerializer looks for the method on the .DeclaringType of your member
Name
, not on the .ReflectedType. This is why it doesn't work.