在 INotifyPropertyChanged 对象中使用带有通用参数的抽象 class

Using Abstract class with Generic parameters in INotifyPropertyChanged object

我正在尝试将 generic/abstract DataSeries 附加到 INotifyPropertyChanged 对象。但是,TX、TY 泛型似乎阻止我按预期使用它。谁能帮帮我?

有关摘要的更多信息 class:https://www.scichart.com/documentation/v5.x/webframe.html#SciChart.Charting~SciChart.Charting.Model.DataSeries.DataSeries%602.html

internal class DataSeriesAbstract : INotifyPropertyChanged
{
    public string dataName;
    public double lastAppendedTimestamp = 0.0f;

    public List<AbstractChartViewModel> subscribers;

    // gives an error that TX and TY cannot be found
    public DataSeries<TX, TY> realData;
    public DataSeries<TX, TY> Data
    {
        get { return realData; }
        set
        {
            realData = value;
            OnPropertyChanged(dataName);
        }
    }
...
}

谢谢,
麦克

至少,您需要将它们作为通用参数添加到 class,加上任何约束

internal class DataSeriesAbstract<TX, TY>: INotifyPropertyChanged
{
   ...

其他资源

Generic Classes (C# Programming Guide)

您的 class 也需要提供泛型。这会将您的 class 声明更改为如下:

internal class DataSeriesAbstract<TX, TY> : INotifyPropertyChanged where TX : IComparable where TY : IComparable

如果您这样做,where 约束将很重要,因为 DataSeries 具有相同的约束。

现在,如果您知道数据系列的具体类型,就可以使用它来代替 TX、TY。例如:

public DataSeries<double, double> realData;
public DataSeries<double, double> Data