首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >派生类上的XMLIgnore-属性

派生类上的XMLIgnore-属性
EN

Stack Overflow用户
提问于 2014-09-08 08:15:08
回答 1查看 2.8K关注 0票数 3

我在基类中有一个用XmlIgnore-attribute标记的属性。当我派生这个类并且不重写(相对于Xml-attributes in interfaces and abstract classes)这个属性时,该属性是否被保留,以便派生类具有带有XmlIgnore-attribute的属性,还是必须重写该属性才能重置该属性?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-18 09:34:09

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

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

显示行为的示例代码:

代码语言:javascript
复制
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());

    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25719916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档