Get XmlEnumAttribute from enum

匿名 (未验证) 提交于 2019-12-03 01:48:02

问题:

I have enum:

public enum Operation {     ///      [System.Xml.Serialization.XmlEnumAttribute("01")]     Item01,      ///      [System.Xml.Serialization.XmlEnumAttribute("02")]     Item02,      ///      [System.Xml.Serialization.XmlEnumAttribute("03")]     Item03,      ///      [System.Xml.Serialization.XmlEnumAttribute("04")]     Item04, } 

How I can get XmlEnumAttribute value?

I'm trying at that:

var res = Operation.Item1; var result = (res.GetType().GetField("Item01").GetCustomAttributes(typeof(XmlEnumAttribute), true)[0] as XmlEnumAttribute).Name; 

May be exists better method?

回答1:

You could create an helper (static) class, with this extension method

public static string GetXmlEnumAttributeValueFromEnum(this TEnum value) where TEnum : struct, IConvertible         {             var enumType = typeof(TEnum);             if (!enumType.IsEnum) return null;//or string.Empty, or throw exception              var member = enumType.GetMember(value.ToString()).FirstOrDefault();             if (member == null) return null;//or string.Empty, or throw exception              var attribute = member.GetCustomAttributes(false).OfType().FirstOrDefault();             if (attribute == null) return null;//or string.Empty, or throw exception             return attribute.Name;         } 

usage

var res = Operation.Item1; var result = res.GetXmlAttributeValueFromEnum(); 


回答2:

You have to use Reflection to get the attribute value:

var value = Operation.Item02;  var attributeValue = ((XmlEnumAttribute)typeof(Operation)                         .GetMember(value.ToString())[0]                         .GetCustomAttributes(typeof(XmlEnumAttribute), false)[0])                         .Name; 


回答3:

Thanks; this is useful to me. I'd like to extend Raphael's answer to a slightly more general scenario. If the enum code is generated from xsd by xsd.exe, not every enum will have the attribute. This may happen when you're using xsd enums to limit strings to a specific list of values, some of which have spaces and some of which don't. For example:

will emit the C# serialization code:

public enum fooEnum {      ///      [System.Xml.Serialization.XmlEnumAttribute("Foo Bar")]     FooBar,      ///      [System.Xml.Serialization.XmlEnumAttribute("Bar Foo")]     BarFoo,      ///      JustPlainFoo,  } 

For this case, client code expecting "JustPlainFoo" will fail. My version of Raphael's answer is then as follows:

public static string XmlEnumToString(TEnum value) where TEnum : struct, IConvertible     {         Type enumType = typeof(TEnum);         if (!enumType.IsEnum)             return null;          MemberInfo member = enumType.GetMember(value.ToString()).FirstOrDefault();         if (member == null)             return null;          XmlEnumAttribute attribute = member.GetCustomAttributes(false).OfType().FirstOrDefault();         if (attribute == null)             return member.Name; // Fallback to the member name when there's no attribute          return attribute.Name;     } 

Finally, I'll note that Rauhotz's commment won't apply to this case; the XmlEnumAttribute won't be there in the generated C# code and you'll just hit the fallback code.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!