XmlSerializer ShouldSerialize* 不适用于基本类型属性

发布于 2025-01-07 10:57:05 字数 501 浏览 3 评论 0原文

如果我为当前类型属性定义 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 技术交流群。

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

发布评论

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

评论(1

深巷少女 2025-01-14 10:57:05

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.

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