簡體   English   中英

.NET - 將多個字符串值映射到XmlEnumAttribute

[英].NET - Mapping more than one string value to XmlEnumAttribute

我有一個枚舉,試圖將字符串解析為枚舉值,這適用於許多情況,但我開始得到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,
}

請注意有兩種類型稱為安全中心,但有時我會收到“安全中心”,有時我會收到“安全中心”。

我的問題是有一種簡化的方法或統一的方法來處理這種情況嗎?

提前致謝。

如果其他人對此有同樣的麻煩。 我通過添加類級屬性[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,
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM