【问题标题】:XMLIgnore-attribute on derived class派生类的 XMLIgnore 属性
【发布时间】:2014-09-08 08:15:08
【问题描述】:

我在标有XmlIgnore-attribute 的基类中有一个属性。当我派生此类并且不覆盖(与Xml-attributes in interfaces and abstract classes相反)此属性时,是否保留此属性以便派生类也具有XmlIgnore-attribute的属性,或者我是否必须覆盖该属性才能重置属性?

【问题讨论】:

    标签: c# inheritance xml-serialization


    【解决方案1】:

    如果您的派生类没有使用 XmlIgnore 属性集覆盖该属性,则该属性将在序列化时被适当地忽略。

    如果您的派生类确实覆盖了该属性,并且忘记设置 XmlIgnore 属性,则该属性将被适当地序列化。

    显示行为的示例代码:

    using System;
    using System.Linq;
    using System.Reflection;
    using System.Xml.Serialization;
    
    [Serializable]
    public abstract class myBaseClass
    {
        [XmlIgnore]
        public virtual bool aBoolean { get; set; }
    
        public int anInt { get; set; }
    }
    
    [Serializable]
    public class myDerivedClass : myBaseClass
    {
        public string derivedString { get; set; }
    }
    
    [Serializable]
    public class overrideXmlIgnore : myBaseClass
    {
        // no XmlIgnore
        public override bool aBoolean
        {
            get
            {
                return base.aBoolean;
            }
            set
            {
                base.aBoolean = value;
            }
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            // build array of types we can serialize/deserialize
            // uses Linq and Reflection namespaces
            Type[] derivedTypes = (from lAssembly in AppDomain.CurrentDomain.GetAssemblies()
                                   from lType in lAssembly.GetTypes()
                                   where typeof(myBaseClass).IsAssignableFrom(lType)
                                   select lType).ToArray();
    
            // build a test object to serialize with XMLIgnore still used
            myDerivedClass m = new myDerivedClass();
            m.aBoolean = true; // this property is ignored by default
            m.derivedString = "test";
    
            // set a file path to serialize to
            string testFilePath = "C:\\temp\\test.xml";
    
            // serialzie the object
            XmlSerializer x = new XmlSerializer(typeof(myBaseClass), derivedTypes);
            System.IO.StreamWriter sw = new System.IO.StreamWriter(testFilePath);
            x.Serialize(sw, m);
            sw.Close();
    
            // deserialize the object
            System.IO.StreamReader sr = new System.IO.StreamReader(testFilePath);
            myBaseClass deserializedObject = (myBaseClass)x.Deserialize(sr);
            sr.Close();
    
            // check the object's properties
            // aBoolean is false, even though the serialized object m set it to true, because of XmlIgnore
            Console.WriteLine("aBoolean = " + deserializedObject.aBoolean.ToString());
    
            // repeat process for the derived class that overrides and does not set XmlIgnore
            overrideXmlIgnore o = new overrideXmlIgnore();
            o.aBoolean = true;
            sw = new System.IO.StreamWriter(testFilePath);
            x.Serialize(sw, o);
            sw.Close();
            sr = new System.IO.StreamReader(testFilePath);
            deserializedObject = (myBaseClass)x.Deserialize(sr);
    
            // check the object's properties
            // aBoolean is true, as we no longer XmlIgnore
            Console.WriteLine("aBoolean = " + deserializedObject.aBoolean.ToString());
    
        }
    }
    

    【讨论】:

    • 实际上 XmlSerializer 中似乎存在一个错误 - 它不尊重 [XmlIgnore] 隐藏(重新引入新)或覆盖属性 - 请参阅dotnetfiddle.net/W9zCSm
    • 刚刚在这里填写了一个错误报告:github.com/dotnet/runtime/issues/44701。另一种方法似乎可以正常工作,也就是如果您在后代的重新引入/覆盖的属性中没有 [XmlIgnore] 虽然祖先具有这样的属性,那么该属性将被序列化 ok
    • 一种解决方法可能是让子类通过辅助方法生成 XmlAttributeOverrides,然后在序列化时,将它们也传递给 XmlSerializer - 请参阅 social.msdn.microsoft.com/Forums/vstudio/en-US/…
    • @GeorgeBirbilis:除非我遗漏了什么,否则“不尊重 XmlIgnore 在隐藏或覆盖的属性”只是重新描述了答案中解释的行为。至于它是否是一个错误,我很难评论。 XmlIgnore 和 XmlSerializer 的文档对于派生类的预期行为并不清楚。 (序列化程序的第一个构造函数参数中的“主”类型是基类型还是派生类型也可能很重要。在答案中,基类型是使用的类型,因为这是序列化基时的常见模式类型。)
    • 我观察到的是,当使用 XMLSerializer 的后代类类型和实例时,您可以覆盖要添加但不能隐藏的内容。请参阅dotnetfiddle.net/Bvbi0N 的解决方法,它还显示了如何注入 cmets(基于 SO 上建议的技术)。其他奇怪的错误也阻碍了我(在那里提到它们),例如当您使用 XmlAnyElement 注入评论时 WCF 窒息,但没有在 XmlAnyElement 属性中指定节点名称
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多