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

我尝试这样做(请参见下面的代码),但是属性[EnumMember(Value = "name")]并没有改变。当我执行.ToJson()时,可以看到该值仍然是“ Monday”而不是“ MONDAY”

[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,
}

最佳答案

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

此更改可从v5.0.3(现在为available on MyGet)获得。

10-06 12:17