我正在使用类似于以下的类反序列化存储在mongodb中的数据。在MongoDB中,SmoothingFn存储为字符串,使用switch语句反序列化。使用此构造函数,反序列化过程失败。使用MongoDB的C#驱动程序设置委托的最佳方法是什么?有办法吗?
编辑:抛出的异常是NullReferenceException:对象引用未设置为对象的实例。
public class MacroModelCfg{
[DataMember]
public string Name;
// More variables
//
[BsonIgnore]
[DataMember]
public Func<TimeSeries, TimeSeries> SmoothingFn = new Func<TimeSeries, TimeSeries>(x => x);
[BsonConstructor]
public MacroModelCfg(string Name, string SmoothingFn)
{
switch (SmoothingFn)
{
case ("MAV2"):
this.SmoothingFn = new Func<TimeSeries, TimeSeries>(x => Fn.SMA(x, 1));
break;
case ("MAV3"):
this.SmoothingFn = new Func<TimeSeries, TimeSeries>(x => Fn.SMA(x, 2));
break;
case ("MAV6"):
this.SmoothingFn = new Func<TimeSeries, TimeSeries>(x => Fn.SMA(x, 5));
break;
case ("EMA2"):
this.SmoothingFn = new Func<TimeSeries, TimeSeries>(x => Fn.EMA(x, 1));
break;
case ("EMA3"):
this.SmoothingFn = new Func<TimeSeries, TimeSeries>(x => Fn.EMA(x, 2));
break;
case ("EMA6"):
this.SmoothingFn = new Func<TimeSeries, TimeSeries>(x => Fn.EMA(x, 5));
break;
default:
throw new Exception("Unrecognized PredictionSmoothingFunction " + SmoothingFn);
}
}
}