【问题标题】:Object serializing/deserializing doesn't work对象序列化/反序列化不起作用
【发布时间】:2012-09-18 19:42:08
【问题描述】:

我有一个扩展方法System.Object 使用Json.Net 序列化和反序列化对象。这是我的扩展方法:

public static void SaveToFile(this object data, string FileName)
{
    using (StreamWriter writer = new StreamWriter(FileName))
    {
        string encode = WpfApplication.Helper.Encrypt(JsonConvert.SerializeObject(data));
        writer.Write(encode);
        writer.Close();
    }
}

public static void LoadFromFile<t>(this object data, string FileName)
{
    using (StreamReader reader = new StreamReader(FileName))
    {

        data = JsonConvert.DeserializeObject<t>(WpfApplication.Helper.Decrypt(reader.ReadToEnd()));
        reader.Close();
    }
}

这是我要反序列化的类:

public class CardPack
{
    #region Properties
    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    private List<FlashCard> cards;

    public List<FlashCard> Cards
    {
        get { return cards; }
        set { cards = value; }
    }

    private bool registered;
    public bool Registered
    {
        get { return registered; }
        set { registered = value; }
    }

    private int currentCardIndex;

    public int CurrentCardIndex
    {
        get { return currentCardIndex; }
        set { currentCardIndex = value; }
    }

    public string RegisterKey { get; set; }

    public string ViewName { get; set; }

    public List<FlashCard> TodayCards { get; set; }
    #endregion
    ~CardPack()
    {
        foreach (FlashCard card in cards)
        {
            card.Check();
        }
        currentCardIndex = 0;
        TodayCards = null;
        this.SaveToFile(string.Format(System.IO.Path.GetDirectoryName(
  System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Split(@"file:\\")[1] + @"\Packs\{0}.json", name));
    }

但每当我反序列化类时,cards 是空的,我不知道如何解决问题。有人可以帮帮我吗?

更新 当我有这个代码时,我发现了错误:

public CardPack(string Name)
    {
        this.name = Name;
        this.LoadFromFile<CardPack>(string.Format(System.IO.Path.GetDirectoryName(
  System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Split(@"file:\\")[1] + @"\Packs\{0}.json", name));
        foreach (var item in cards)
        {
            if (item.NextTime == null)
            {
                int a = 0;
            }
        }
        TodayCards = cards.Where(c => c.NextTime.Date == DateTime.Today).ToList();
    }

因为应用程序在尝试运行时关闭foreach (var item in cards)! 我问了here,发现cards是空的!

update2 我用稍微不同的结构序列化了CardPack 对象。在之前的结构中,Cards 属性是只读的。

【问题讨论】:

    标签: c# c#-4.0 serialization json.net deserialization


    【解决方案1】:

    我发现扩展方法中的data = JsonConvert.DeserializeObject&lt;t&gt;(WpfApplication.Helper.Decrypt(reader.ReadToEnd())); 不会更改为“数据”类,然后CardPack 中的Cards 始终为空。稍后我会问一个问题,看看为什么我不能从它的扩展方法中设置类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-06
      • 2021-01-26
      • 2020-10-16
      • 2014-08-11
      • 1970-01-01
      • 1970-01-01
      • 2012-04-15
      • 1970-01-01
      相关资源
      最近更新 更多