zrabzdn
zrabzdn

Reputation: 995

Get XmlEnumAttribute from enum

I have enum:

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

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("02")]
    Item02,

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("03")]
    Item03,

    /// <remarks/>
    [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?

Upvotes: 22

Views: 19983

Answers (3)

Eric Hirst
Eric Hirst

Reputation: 1161

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:

<xs:simpleType name="fooEnum">
 <xs:restriction base="xs:string">
  <xs:enumeration value="Foo Bar" />
  <xs:enumeration value="Bar Foo" />
  <xs:enumeration value="JustPlainFoo" />
 </xs:restriction>
</xs:simpleType>

will emit the C# serialization code:

public enum fooEnum {

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("Foo Bar")]
    FooBar,

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("Bar Foo")]
    BarFoo,

    /// <remarks/>
    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>(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<XmlEnumAttribute>().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.

Upvotes: 6

Rapha&#235;l Althaus
Rapha&#235;l Althaus

Reputation: 60503

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

public static string GetXmlEnumAttributeValueFromEnum<TEnum>(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<XmlEnumAttribute>().FirstOrDefault();
    if (attribute == null) return null;//or string.Empty, or throw exception
    return attribute.Name;
}

usage

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

Upvotes: 29

MarcinJuraszek
MarcinJuraszek

Reputation: 125650

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;

Upvotes: 10

Related Questions