gpt4 book ai didi

c# - .NET - 将多个字符串值映射到 XmlEnumAttribute

转载 作者:行者123 更新时间:2023-11-30 18:20:59 26 4
gpt4 key购买 nike

我有一个枚举试图将字符串解析为枚举值,这适用于许多情况,但我开始获取 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,
}

请注意有两种类型称为 Secure central,但有时我会收到“secure central”,有时会收到“Secure central”。

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

提前致谢。

最佳答案

如果其他人遇到同样的问题。我通过添加类级别属性 [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,
}

关于c# - .NET - 将多个字符串值映射到 XmlEnumAttribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36481577/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com