Get XmlEnumAttribute from enum

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

I have enum:

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

    /// 
          


        
3条回答
  •  醉酒成梦
    2020-12-30 03:54

    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;
    

提交回复
热议问题