2012-10-21 69 views
0

當我試圖序列化對象的通用列表時,由於派生類使用的任何變量而導致一些數據丟失。我曾經在派生類中使用這些變量,但由於基類已被序列化,我假定派生變量將被忽略。當派生類和基類繼承發生XMLSerialization時的數據丟失

驗證碼:

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

任何人有這是爲什麼發生的任何想法?

回答

0

由於只有公共屬性和字段可以由XMLSerializer序列化,所以您必須具有公用字段而不是受保護字段。