gpt4 book ai didi

c# - 如何在c#中自动更新属性

转载 作者:行者123 更新时间:2023-12-02 11:07:48 26 4
gpt4 key购买 nike

我有一个 Json 对象。每当任何其他属性设置(更改)时,我想更新其lastUpdate属性。

如何在不从调用函数调用 lastUpdate 属性的情况下解决此问题?

public class TickerData
{
public string symbol { get; set; }
public string exchange { get; set; }
public SecurityType securityType { get; set; }
public string Currency { get; set; } // convert to enum later

public strategy entryStrategy { get; set; }
public OHLCV ohlcv { get; set; }
public HistoricalDataEventArgs[] historicalData { get; set; }
public DateTime date { get; set; }
public decimal open { get; set; }
public decimal high { get; set; }
public decimal low { get; set; }

public decimal close { get; set; }
public DateTime lastUpdate { get; set; }
}

最佳答案

一种选择是:

  1. 添加tickerChanged event到你的类(class)
  2. 为您的属性提供您读取/写入的支持字段
  3. 每次设置 lastUpdate 以外的支持字段时,调用 tickerChanged()
  4. tickerChanged 添加一个事件处理程序,用于更新 lastUpdate 属性

    public class TickerData
    {
    private event Action tickerChanged;

    private string _symbol;
    public string symbol
    {
    get
    {
    return _symbol;
    }
    set
    {
    _symbol = value;
    tickerChanged();
    }
    }

    private string _exchange;
    public string exchange
    {
    get
    {
    return _exchange;
    }
    set
    {
    _exchange= value;
    tickerChanged();
    }
    }

    // ... continue the pattern ...

    public DateTime lastUpdate { get; private set; }

    public TickerData()
    {
    // register an event handler that
    // updates the lastUpdate property
    tickerChanged += () => lastUpdate = DateTime.Now;
    }
    }

关于c# - 如何在c#中自动更新属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23941890/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com