0

I'm in the procenss of deserializing data stored in mongodb using a class similar to the following. Within MongoDB, SmoothingFn is stored as a string, the deserialized using the switch statement. Using this constructor, the de-serialization process fails. What's the best way to set a delegate using MongoDB's C# driver? Is there a way?

EDIT: The exception that's being thrown is NullReferenceException: Object reference not set to an instance of an object.

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);
        }
    }


}
John Saunders
  • 157,405
  • 24
  • 229
  • 388
CodeOcelot
  • 2,944
  • 3
  • 18
  • 23

0 Answers0