【问题标题】:.NET - Mapping more than one string value to XmlEnumAttribute.NET - 将多个字符串值映射到 XmlEnumAttribute
【发布时间】:2016-04-07 16:05:07
【问题描述】:

我有一个枚举,它试图将字符串解析为枚举值,这适用于许多情况,但我开始获取 SOAP 值,这些值试图解析我的一个枚举的相同类型,但字符串的大小写是不同的,所以我做了一个这样的快速解决方案:

public enum RepoType
{

    /// <remarks/>
    local,

    /// <remarks/>
    central,

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("secure central")]        
    securecentral,

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("Secure central")]
    Securecentral,

    /// <remarks/>
    profiler,
}

请注意有两种类型称为安全中心,但有时我收到“安全中心”,有时我收到“安全中心”。

我的问题是有一种简化的方式还是统一的方式来处理这种情况?

提前致谢。

【问题讨论】:

  • 你好,你是怎么解决的?
  • 我没解决。

标签: c# .net enums


【解决方案1】:

如果其他人也有同样的问题。我通过添加类级别属性[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))] 并将属性属性添加到各个字段[EnumMember(Value = "secure central")] 来解决它

请参阅下面的 OP 解决方案。添加的属性比较多,不需要的去掉就好了。

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
[System.SerializableAttribute()]

public enum RepoType
{

    /// <remarks/>
    local,

    /// <remarks/>
    central,

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("secure central")]    
    [EnumMember(Value = "secure central")]    
    securecentral,

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("Secure central")]
    [EnumMember(Value = "Secure central")]
    Securecentral,

    /// <remarks/>
    profiler,
}

【讨论】:

    猜你喜欢
    • 2012-08-21
    • 2018-07-09
    • 2019-01-25
    • 2021-11-09
    • 2019-06-09
    • 1970-01-01
    • 2012-01-23
    • 2021-01-15
    • 2017-10-27
    相关资源
    最近更新 更多