Get XmlEnumAttribute from enum

前端 未结 3 1778
感动是毒
感动是毒 2020-12-30 02:52

I have enum:

public enum Operation {
    /// 
    [System.Xml.Serialization.XmlEnumAttribute(\"01\")]
    Item01,

    /// 
          


        
3条回答
  •  执笔经年
    2020-12-30 03:42

    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(this 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.

提交回复
热议问题