使用派生类和基类继承进行XMLSerialization时的数据丢失

时间:2012-10-21 06:00:22

标签: c#-4.0 windows-runtime

当我尝试序列化一个通用的对象列表时,我会得到一些数据丢失,以及派生类使用的任何变量。我以前在派生类中有这些变量,但由于基类被序列化,我认为派生变量会被忽略。

守则:

[XmlInclude(typeof(AchievementStock))]
    [XmlInclude(typeof(AchievementCash))]
    [XmlInclude(typeof(AchievementStockSpecify))]
    public abstract class Achievement : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected string _title;
        protected string _description;
        protected int _cashValue;
        protected bool _completed;

        //Cash
        protected int _amountCash;
        protected bool _greater;

        //Stocks
        protected int _amountStocks;

        //StockSpecify
        protected string _stockName;
        protected int _amount;


        public abstract void CheckAchievement(AssetManager assetManager, AchievementManager achievementManager);

        public Achievement()
        {
        }

        public Achievement(string title, string description, int cashValue, bool completed)
        {
            Title = title;
            Description = description;
            _cashValue = cashValue;
            _completed = completed;
        }

        public void PropertyChangedEvent(string assetName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(assetName));
            }
        }

    }

    public class AchievementCash : Achievement
    {


        public AchievementCash(string title, string description, int cashValue, bool completed, int amountCash, bool greater)
            : base(title, description, cashValue, completed)
        {
            _amountCash = amountCash;
            _greater = greater;
        }

        public AchievementCash()
        {
        }

        public override void CheckAchievement(AssetManager assetManager, AchievementManager achievementManager)
        {


        }
    }

    public class AchievementStock : Achievement
    {


        public AchievementStock(string title, string description, int cashValue, bool completed, int amountStocks)
            : base(title, description, cashValue, completed)
        {
            _amountStocks = amountStocks;
        }

        public AchievementStock()
        {
        }

        public override void CheckAchievement(AssetManager assetManager, AchievementManager achievementManager)
        {
        }
    }

    public class AchievementStockSpecify : Achievement
    {
        public AchievementStockSpecify(string title, string description, int cashValue, bool completed, string stockName, int amount)
            : base(title, description, cashValue, completed)
        {
            _stockName = stockName;
            _amount = amount;
        }

        public AchievementStockSpecify()
        {
        }

        public override void CheckAchievement(AssetManager assetManager, AchievementManager achievementManager)
        {
        }
}

变为默认值的变量是:

        //Cash
        protected int _amountCash;
        protected bool _greater;

        //Stocks
        protected int _amountStocks;

        //StockSpecify
        protected string _stockName;
        protected int _amount;

任何人都对为什么会这样做有任何想法?

1 个答案:

答案 0 :(得分:0)

您必须拥有公共字段而不是受保护字段,因为只有公共属性和字段可以由XMLSerializer序列化。