反序列化yaml文件时出错

getting error while deserializing yaml file

序列化没有问题,反序列化抛出如下错误。我分享了所有相关的 类 和错误。我应该怎么做才能反序列化这个文件。

yaml 文件(由 yaml 序列化创建):

  Edits:   0:
        Id: 20
        Type: AddOrUpdate
        Operation:
          book:
            ElementName: a
            ArithmeticExpression: asd
            ConditionExpression: a

c# 类 :

 public class DbContext
        {
            public Dictionary<int, DbModel> Edits { get; set; }
        }



      public class DbModel
        {
            public int Id { get; set; }
            public string Type { get; set; }
            public Dictionary<string, Operation> Operation { get; set; }


            public DbModel(int id, string type, Dictionary<string, Operation> op)
            {
                Id = id;
                Type = type;
                Operation = op;
            }
        }
    public class Operation
    {
        public string ElementName { get; set; }
        public string ArithmeticExpression { get; set; }
        public string ConditionExpression { get; set; }

        public Operation(string elemName, string ArithmetticExp, string ConditionExp)
        {
            ElementName = elemName;
            ArithmeticExpression = ArithmetticExp;
            ConditionExpression = ConditionExp;
        }

    }

class DatabaseOperation
{
    public static DbContext DbContext { get; set; }
    public const string yamlEditString = "C:\Data\setting.yml";

    static DatabaseOperation()
    {
        DbContext = ReadAllDataFromFile().Result;
    }

    public static Task<DbContext> ReadAllDataFromFile()
    {
        DbContext dbCon = new DbContext();
        if (File.Exists(yamlEditString))
        {

            var deserializer = new Deserializer();

            string readedData = File.ReadAllText(yamlEditString);
            //dbCon.Edits = deserializer.Deserialize<List<DbModel>>(readedData);
            try
            {
                dbCon = deserializer.Deserialize<DbContext>(readedData);
            }
            catch (Exception ex)
            {

                throw;
            }

        }
        else
        {
            dbCon.Edits = new Dictionary<int, DbModel>();
        }
        return Task.FromResult(dbCon);
    }
    public static Task WriteAllDataToFile(DbContext dbCon)
    {
        var serializer = new Serializer();
        File.WriteAllText(yamlEditString, serializer.Serialize(dbCon));
    }
}

getting this error : (Line: 7, Col: 9, Idx: 86) - (Line: 7, Col: 9, Idx: 86): Exception during deserialization

"YamlDotNet"

内部异常:

Failed to create an instance of type 'YamlEdit.Operation'.

at YamlDotNet.Serialization.ObjectFactories.DefaultObjectFactory.Create(Type type) at YamlDotNet.Serialization.NodeDeserializers.ObjectNodeDeserializer.YamlDotNet.Serialization.INodeDeserializer.Deserialize(IParser parser, Type expectedType, Func`3 nestedObjectDeserializer, Object& value) at YamlDotNet.Serialization.ValueDeserializers.NodeValueDeserializer.DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)

堆栈跟踪:

   at YamlDotNet.Serialization.ValueDeserializers.NodeValueDeserializer.DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
   at YamlDotNet.Serialization.ValueDeserializers.AliasValueDeserializer.DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
   at YamlDotNet.Serialization.ValueDeserializers.NodeValueDeserializer.<>c__DisplayClass3_0.<DeserializeValue>b__0(IParser r, Type t)
   at YamlDotNet.Serialization.NodeDeserializers.DictionaryNodeDeserializer.DeserializeHelper(Type tKey, Type tValue, IParser parser, Func`3 nestedObjectDeserializer, IDictionary result)
   at YamlDotNet.Serialization.NodeDeserializers.DictionaryNodeDeserializer.YamlDotNet.Serialization.INodeDeserializer.Deserialize(IParser parser, Type expectedType, Func`3 nestedObjectDeserializer, Object& value)
   at YamlDotNet.Serialization.ValueDeserializers.NodeValueDeserializer.DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
   at YamlDotNet.Serialization.ValueDeserializers.AliasValueDeserializer.DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
   at YamlDotNet.Serialization.ValueDeserializers.NodeValueDeserializer.<>c__DisplayClass3_0.<DeserializeValue>b__0(IParser r, Type t)
   at YamlDotNet.Serialization.NodeDeserializers.ObjectNodeDeserializer.YamlDotNet.Serialization.INodeDeserializer.Deserialize(IParser parser, Type expectedType, Func`3 nestedObjectDeserializer, Object& value)
   at YamlDotNet.Serialization.ValueDeserializers.NodeValueDeserializer.DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
   at YamlDotNet.Serialization.ValueDeserializers.AliasValueDeserializer.DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
   at YamlDotNet.Serialization.Deserializer.Deserialize(IParser parser, Type type)
   at YamlDotNet.Serialization.Deserializer.Deserialize[T](String input)
   at YamlEdit.DatabaseOperation.ReadAllDataFromFile() in D:\Repositories\YamlEdit\DatabaseOperation.cs:line 35

您需要向 YamlEdit.Operation 添加一个无参数构造函数。 YamlDotNet 不知道如何传递构造函数参数。