【问题标题】:How to ignore non-serializable properties from being cached using Akavache?如何忽略使用 Akavache 缓存的不可序列化属性?
【发布时间】:2014-12-28 13:50:23
【问题描述】:

我有一个要使用 Akavache 缓存的自定义类。此类包含一个不可序列化的属性(它是 ParseFile 对象)。当我尝试使用 GetAndFetchLatest 缓存/获取此自定义类的列表时,它不起作用。我认为这可能是由一个不可序列化的属性引起的。尝试了 JsonIgnore 属性,但没有帮助。

问题是,我如何告诉 Akavache 缓存自定义类列表中的所有内容,但不可序列化的属性除外?自定义类的示例在这里,我希望它忽略 Photo 属性:

[ParseClassName("User")]
public class User : ParseObject
{
    [ParseFieldName("name")]
    public string Name
    { 
        get { return GetProperty<string>(); }
        set { SetProperty<string>(value); }
    }

    [ParseFieldName("photo")]
    public ParseFile Photo
    { 
        get { return GetProperty<ParseFile>(); }
        set { SetProperty<ParseFile>(value); }
    }
}

【问题讨论】:

    标签: c# android xamarin akavache


    【解决方案1】:

    使用[DataContract][IgnoreDataMember] 防止字段被序列化

    【讨论】:

    • 还是不行 :( 可能是因为 Akavache 没有使用 System.Runtime.Serialization 来序列化它。
    • JSON.NET 尊重 System.Runtime.Serialization 属性,你确定你用DataContract所有成员标记你的类吗?
    • 我做了,下面的代码就是我所做的。但是即使在此修改之后,看起来 User 类也没有被缓存。 [DataContract] [ParseClassName("User")] public class User { [DataMember] [ParseFieldName("name")] public string Name { get { return GetProperty&lt;string&gt;(); } set { SetProperty&lt;string&gt;(value); } } [IgnoreDataMember] [ParseFieldName("photo")] public ParseFile Photo { get { return GetProperty&lt;ParseFile&gt;(); } set { SetProperty&lt;ParseFile&gt;(value); } } }
    • 顺便说一句,Akavache 使用 Sqlite3 作为其后端。
    • 这是什么“GetProperty”和“SetProperty”业务?
    【解决方案2】:

    默认的 Json.Net 行为是序列化所有公共字段和属性,但标有 [JsonIgnoreAttribute] 属性的除外。 参考:http://james.newtonking.com/archive/2009/10/23/efficient-json-with-json-net-reducing-serialized-json-size

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-07
      • 1970-01-01
      • 1970-01-01
      • 2022-08-11
      • 2016-08-27
      相关资源
      最近更新 更多