通过发送XmlEnumAttribute c#来获取枚举的元素?

时间:2017-03-24 01:52:39

标签: c# xml enums

我通常不必提问,因为大多数时候我在互联网上找到了我需要的东西,但现在我还没找到办法解决这个问题:

想象一下,我在这个枚举中有50000个枚举元素:

public enum myEnum
{
   [System.Xml.Serialization.XmlEnumAttribute("01010101")]
   Item01010101,
   [System.Xml.Serialization.XmlEnumAttribute("10101500")]
   Item10101500
}

我想要做的是通过传递元素的XmlEnumAttribute的字符串值来获取元素值,例如

object.ItsEnumValue = getEnumElement("01010101");

// Function which will return the element of the enum.
public myEnum getEnumElement(string xmlAttributeValue)
{
    // The function should return the enum element, not using a switch statement
    return myEnum.Item01010101;
}

有没有办法在没有switch语句的情况下执行此操作? 我希望你能帮助我,谢谢。

2 个答案:

答案 0 :(得分:3)

您有几种方法可以解决此问题。

首先,你可以使用反射来使用typeof(TEnum).GetFields(BindingFlags.Public | BindingFlags.Static)循环遍历所有枚举值,类似于在this answer中完成的方式:

public static partial class XmlEnumExtensions
{
    public static TEnum FromReflectedXmlValue<TEnum>(this string xml) where TEnum : struct, IConvertible, IFormattable
    {
        // Adapted from https://stackoverflow.com/questions/35294530/c-sharp-getting-all-enums-value-by-attribute
        var obj = (from field in typeof(TEnum).GetFields(BindingFlags.Public | BindingFlags.Static)
                   from attr in field.GetCustomAttributes<XmlEnumAttribute>()
                   where attr != null && attr.Name == xml
                   select field.GetValue(null)).SingleOrDefault();
        if (obj != null)
            return (TEnum)obj;

        // OK, maybe there is no XmlEnumAttribute override so match on the name.
        return (TEnum)Enum.Parse(typeof(TEnum), xml, false);
    }
}

然后使用它:

obj.ItsEnumValue = "01010101".FromReflectedXmlValue<myEnum>();

但是,当应用[Flags]属性时,这将不起作用。例如,给出以下枚举:

[Flags]
public enum myFlagsEnum
{
    [System.Xml.Serialization.XmlEnumAttribute("Flag0")]
    Zero = (1 << 0),
    [System.Xml.Serialization.XmlEnumAttribute("Flag1")]
    One = (1 << 1),
}

从值myFlagsEnum.Zero | myFlagsEnum.One开始,XmlSerializer将生成以下组合字符串,纯粹通过反射找不到它:Flag0 Flag1。当多个XmlEnumAttribute属性应用于给定的枚举值,或者只有一些枚举值已应用XmlEnumAttribute时,也不清楚会发生什么。

为了处理包括上述内容在内的所有可能的边缘情况,我建议使用以下第二种方法直接反序列化 XML:

public static partial class XmlExtensions
{
    static XmlExtensions()
    {
        noStandardNamespaces = new XmlSerializerNamespaces();
        noStandardNamespaces.Add("", ""); // Disable the xmlns:xsi and xmlns:xsd attributes.
    }

    readonly static XmlSerializerNamespaces noStandardNamespaces;
    internal const string RootNamespace = "XmlExtensions";
    internal const string RootName = "Root";

    public static TEnum FromXmlValue<TEnum>(this string xml) where TEnum : struct, IConvertible, IFormattable
    {
        var element = new XElement(XName.Get(RootName, RootNamespace), xml);
        return element.Deserialize<XmlExtensionsEnumWrapper<TEnum>>().Value;
    }

    public static T Deserialize<T>(this XContainer element, XmlSerializer serializer = null)
    {
        using (var reader = element.CreateReader())
        {
            object result = (serializer ?? new XmlSerializer(typeof(T))).Deserialize(reader);
            if (result is T)
                return (T)result;
        }
        return default(T);
    }

    public static string ToXmlValue<TEnum>(this TEnum value) where TEnum : struct, IConvertible, IFormattable
    {
        var root = new XmlExtensionsEnumWrapper<TEnum> { Value = value };
        return root.SerializeToXElement().Value;
    }

    public static XElement SerializeToXElement<T>(this T obj)
    {
        return obj.SerializeToXElement(null, noStandardNamespaces); // Disable the xmlns:xsi and xmlns:xsd attributes by default.
    }

    public static XElement SerializeToXElement<T>(this T obj, XmlSerializer serializer, XmlSerializerNamespaces ns)
    {
        var doc = new XDocument();
        using (var writer = doc.CreateWriter())
            (serializer ?? new XmlSerializer(obj.GetType())).Serialize(writer, obj, ns);
        var element = doc.Root;
        if (element != null)
            element.Remove();
        return element;
    }
}

[XmlRoot(XmlExtensions.RootName, Namespace = XmlExtensions.RootNamespace)]
[XmlType(IncludeInSchema = false)]
public class XmlExtensionsEnumWrapper<TEnum>
{
    [XmlText]
    public TEnum Value { get; set; }
}

这保证了在所有情况下与XmlSerializer的兼容性 - 通过实际使用它。然后同样做:

obj.ItsEnumValue = "01010101".FromXmlValue<myEnum>();

示例fiddle

答案 1 :(得分:0)

感谢dbc,第一种方法非常适合我需要做的事情,:)我真的很感谢你的帮助,我创建了这个类:

public static partial class XmlEnumExtensions
{
    public static Nullable<TEnum> FromReflectedXmlValue<TEnum>(this string xml) where TEnum : struct, IConvertible, IFormattable
    {
        try
        {
            var obj = (from field in typeof(TEnum).GetFields(BindingFlags.Public | BindingFlags.Static)
                       from attr in field.GetCustomAttributes<XmlEnumAttribute>()
                       where attr != null && attr.Name == xml
                       select field.GetValue(null)).SingleOrDefault();
            if (obj != null)
                return (TEnum)obj;
             // OK, maybe there is no XmlEnumAttribute override so match on the name.
            return (TEnum)Enum.Parse(typeof(TEnum), xml, false);

        }
        catch (ArgumentException ex)
        {
            throw new ApplicationException("Error: " + ex.Message);
        }
    }
}

然后我就像这样调用方法FromReflectedXmlValue:

var obj4 = XmlEnumExtensions.FromReflectedXmlValue<myEnum>("theXmlAtributeValue");