zhao
2021-07-19 8347f2fbddbd25369359dcb2da1233ac48a19fdc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
 
namespace Novacode
{
    internal static class XElementHelpers
    {
        /// <summary>
        /// Get value from XElement and convert it to enum
        /// </summary>
        /// <typeparam name="T">Enum type</typeparam>        
        internal static T GetValueToEnum<T>(XElement element)
        {
            if (element == null)
                throw new ArgumentNullException("element");
 
            String value = element.Attribute(XName.Get("val")).Value;
            foreach (T e in Enum.GetValues(typeof(T)))
            {
                FieldInfo fi = typeof(T).GetField(e.ToString());
                if (fi.GetCustomAttributes(typeof(XmlNameAttribute), false).Count() == 0)
                    throw new Exception(String.Format("Attribute 'XmlNameAttribute' is not assigned to {0} fields!", typeof(T).Name));
                XmlNameAttribute a = (XmlNameAttribute)fi.GetCustomAttributes(typeof(XmlNameAttribute), false).First();
                if (a.XmlName == value)
                    return e;
            }
            throw new ArgumentException("Invalid element value!");
        }
 
        /// <summary>
        /// Convert value to xml string and set it into XElement
        /// </summary>
        /// <typeparam name="T">Enum type</typeparam> 
        internal static void SetValueFromEnum<T>(XElement element, T value)
        {
            if (element == null)
                throw new ArgumentNullException("element");
            element.Attribute(XName.Get("val")).Value = GetXmlNameFromEnum<T>(value);
        }
 
        /// <summary>
        /// Return xml string for this value
        /// </summary>
        /// <typeparam name="T">Enum type</typeparam> 
        internal static String GetXmlNameFromEnum<T>(T value)
        {
            if (value == null)
                throw new ArgumentNullException("value");
 
            FieldInfo fi = typeof(T).GetField(value.ToString());
            if (fi.GetCustomAttributes(typeof(XmlNameAttribute), false).Count() == 0)
                throw new Exception(String.Format("Attribute 'XmlNameAttribute' is not assigned to {0} fields!", typeof(T).Name));
            XmlNameAttribute a = (XmlNameAttribute)fi.GetCustomAttributes(typeof(XmlNameAttribute), false).First();
            return a.XmlName;
        }
    }
 
    /// <summary>
    /// This attribute applied to enum's fields for definition their's real xml names in DocX file.
    /// </summary>
    /// <example>
    /// public enum MyEnum
    /// {
    ///    [XmlName("one")] // This means, that xml element has 'val="one"'
    ///    ValueOne,
    ///    [XmlName("two")] // This means, that xml element has 'val="two"'
    ///    ValueTwo
    /// }
    /// </example>
    [AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
    internal sealed class XmlNameAttribute : Attribute
    {
        /// <summary>
        /// Real xml name
        /// </summary>
        public String XmlName { get; private set; }
 
        public XmlNameAttribute(String xmlName)
        {
            XmlName = xmlName;
        }
    }
}