【问题标题】:Can I have customize getter and setter for Realm in c#?我可以在 c# 中为 Realm 自定义 getter 和 setter 吗?
【发布时间】:2020-10-15 15:08:27
【问题描述】:

我有一个RealmObject 扩展类。它的一些属性是这样声明的:

public String Name { get; set; }
public int Age { get; set; }

当我保存对象时,这些存储在 Realm 数据库中。对于另一个,我需要一个这样的自定义设置器:

private double _amount;
public double Amount
{
    get { return _amount; }
    set
    {
        _amount = value;
        NotifyPropertyChanged();
    }
}

而且这个没有被存储。有人可以帮我吗?

提前致谢

【问题讨论】:

    标签: c# realm


    【解决方案1】:

    Realm 将仅存储具有自动 getter 和 setter 的属性,因为它使用编译时 IL 生成来用对数据库的调用来替换实现。如果要自定义 getter 和 setter,则需要将字段定义为属性:

    private double _amount { get; set; }
    public double Amount
    {
        get { return _amount; }
        set { ... }
    }
    

    Realm 将使用_amount 属性,而您的应用可以使用公共Amount 之一。

    附带说明,您不需要调用 NotifyPropertyChanged 来获取持久属性,因为 Realm 已经为您处理了 INotifyPropertyChanged 事件。所以public double Amount { get; set; } 足以引发属性更改通知。

    【讨论】:

      猜你喜欢
      • 2023-03-12
      • 2017-01-28
      • 2019-04-12
      • 2017-10-31
      • 1970-01-01
      • 2012-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多