在YamlDotNet中,如何反序列化仅限getter的属性?

时间:2018-02-08 03:31:01

标签: c# deserialization getter yamldotnet

我想序列化/反序列化只具有公共getter-only属性的readonly私有字段的对象,其readonly字段在对象的构造函数中设置。但是,以下代码在单元测试中失败:

using System.ComponentModel;
using FluentAssertions;
using Xunit;
using YamlDotNet.Serialization;

namespace Utilities.ComputerInventory {

    public class CPU {
        readonly CPUMaker cPUMaker;
        public CPU() : this(CPUMaker.Generic) { }
        public CPU(CPUMaker cPUMaker) {
            this.cPUMaker = cPUMaker;
        }
        public CPUMaker CPUMaker => cPUMaker;
    }

    public enum CPUMaker {
        [Description("Generic")]
        Generic,
        [Description("Intel")]
        Intel,
        [Description("AMD")]
        AMD
    }
}

namespace Utilities.ComputerInventory.UnitTests {

    public class ComputerInventoryUnitTests001 {

        [Fact]
        public void CPUDeserializeFromYAMLSO() {
            Serializer serializer = new SerializerBuilder().Build();
            Deserializer deserializer = new DeserializerBuilder().Build();
            CPU cPU = new CPU(CPUMaker.Intel);
            CPU cPUAfterRoundTrip = deserializer.Deserialize<CPU>(serializer.Serialize(cPU));
            cPUAfterRoundTrip.Should().Be(cPU);
        }
    }
}

此操作失败并显示“测试失败”消息

Message: YamlDotNet.Core.YamlException : (Line: 1, Col: 1, Idx: 0) - (Line: 1, Col: 1, Idx: 0): Exception during deserialization
---- System.Runtime.Serialization.SerializationException : Property 'CPUMaker' not found on type 'Utilities.ComputerInventory.CPU'.

显示堆栈跟踪

Test Name:  CPUDeserializeFromYAMLSO
Test FullName:  Utilities.ComputerInventory.UnitTests.ComputerInventoryUnitTests001.CPUDeserializeFromYAMLSO
Test Source:    <obfuscated>\test1.cs
Test Outcome:   Failed
Test Duration:  0:00:00.11

Result StackTrace:  
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(TextReader input, Type type)
   at YamlDotNet.Serialization.Deserializer.Deserialize[T](String input)
   at Utilities.ComputerInventory.UnitTests.ComputerInventoryUnitTests001.CPUDeserializeFromYAMLSO() in <obfuscated>\test1.cs:line 43
----- Inner Stack Trace -----
   at YamlDotNet.Serialization.TypeInspectors.TypeInspectorSkeleton.GetProperty(Type type, Object container, String name, Boolean ignoreUnmatched)
   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)
Result Message: 
YamlDotNet.Core.YamlException : (Line: 1, Col: 1, Idx: 0) - (Line: 1, Col: 1, Idx: 0): Exception during deserialization
---- System.Runtime.Serialization.SerializationException : Property 'CPUMaker' not found on type 'Utilities.ComputerInventory.CPU'.

只需序列化CPU对象的cPU实例就会产生如下所示的YAML:"CPUMaker: Intel\r\n"

似乎有一个类似的SO问题(YamlDotNet can not find property),问到12/23/16(13个月前),但没有人回答或评论。

总结:使用YamlDotNet,使用readonly字段支持的仅具有getter属性的对象序列化/反序列化的正确方法是什么?

TIA提供任何帮助!

0 个答案:

没有答案