0

当我尝试序列化对象的通用列表时,我会丢失一些数据,其中包含派生类使用的任何变量。我曾经在派生类中有这些变量,但由于基类正在序列化,我认为派生变量会被忽略。

编码:

[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;

有人对为什么会发生这种情况有任何想法吗?

4

1 回答 1

0

您必须有公共字段而不是受保护的字段,因为只有公共属性和字段才能被 XMLSerializer 序列化。

于 2012-10-21T22:55:27.290 回答