如何使用 ServiceStack.Text 中的 EnumMember 属性反序列化可为 null 的枚举?

How to deserialize nullable enum with EnumMember attribute in ServiceStack.Text?

这是我之前关于 ServiceStack.Text: , . As seen by checking answers to those questions, it is possible to serialize and deserialize enums based on their data contract's and EnumMember 属性的两个问题的后续问题。但是可空枚举,即 EnumType? 类型呢?使用 ServiceStack.Text,是否可以使用 EnumMember 属性序列化这些?使用ServiceStack.Text 5.2.0,代码如下:

namespace TestNameSpace
{
    using ServiceStack;
    using System;
    using System.Runtime.Serialization;

    class TestClass
    {
        [DataContract]
        enum TestEnum
        {
            [EnumMember(Value = "enum_value")]
            EnumValue = 0,
        }

        static void Main(string[] args)
        {
            TestEnum? nullableEnum = TestEnum.EnumValue;
            Console.WriteLine($"nullableEnum.ToJson: {nullableEnum.ToJson()}");
            Console.WriteLine($"nullableEnum.ToCsv: {nullableEnum.ToCsv()}");
            Console.WriteLine($"nullableEnum.ToJsv: {nullableEnum.ToJsv()}");
            Console.WriteLine($"nullableEnum.ToXml: {nullableEnum.ToXml()}");
        }
    }
}

打印以下内容:

nullableEnum.ToJson: "EnumValue"
nullableEnum.ToCsv: EnumValue
nullableEnum.ToJsv: EnumValue
nullableEnum.ToXml: <?xml version="1.0" encoding="utf-8"?><TestClass.TestEnum xmlns="http://schemas.datacontract.org/2004/07/TestNameSpace">enum_value</TestClass.TestEnum>

虽然我希望所有输出格式都有 enum_value。有可能用 ServiceStack.Text 实现吗?

现在应该从 this commit. This change is available from v5.2.1 that's now available on MyGet 解决。