本文介绍了XmlSerializer ShouldSerialize *不适用于基本类型属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我为当前的type属性定义ShouldSerialize *,它将起作用.但是,如果我指定基本类型属性,它将无法正常工作.* Specified

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
        }
    }

推荐答案

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.

这篇关于XmlSerializer ShouldSerialize *不适用于基本类型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 23:25