ServiceStack将枚举值序列化为名称以外的字符串

时间:2018-04-08 07:03:31

标签: c# serialization servicestack

我需要将枚举值序列化为名称以外的字符串。

我尝试过这样做(请参阅下面的代码),但属性[EnumMember(Value = "name")]并没有改变任何内容。当我.ToJson()时,我可以看到价值仍然是"星期一"而不是"星期一"

[DataContract]
public enum Day
{
    [EnumMember(Value = "MONDAY")]
    Monday,
    [EnumMember(Value = "TUESDAY")]
    Tuesday,
    [EnumMember(Value = "WEDNESDAY")]
    Wednesday,
    [EnumMember(Value = "THURSDAY")]
    Thursday,
    [EnumMember(Value = "FRIDAY")]
    Friday,
    [EnumMember(Value = "SATURDAY")]
    Saturday,
    [EnumMember(Value = "SUNDAY")]
    Sunday,
}

1 个答案:

答案 0 :(得分:3)

我刚刚在this commit中添加了对[EnumMember]自定义的支持,您的示例将按预期工作。

此更改现在可从v5.0.3获取,现在为available on MyGet