Exemplo n.º 1
0
        internal static string GetXmlNameFromEnum <T>(T value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            FieldInfo field = typeof(T).GetField(value.ToString());

            if (field.GetCustomAttributes(typeof(XmlNameAttribute), inherit: false).Count() == 0)
            {
                throw new Exception($"Attribute 'XmlNameAttribute' is not assigned to {typeof(T).Name} fields!");
            }
            XmlNameAttribute xmlNameAttribute = (XmlNameAttribute)field.GetCustomAttributes(typeof(XmlNameAttribute), inherit: false).First();

            return(xmlNameAttribute.XmlName);
        }
Exemplo n.º 2
0
        /// <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(nameof(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);
        }
Exemplo n.º 3
0
        internal static T GetValueToEnum <T>(XElement element)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }
            string value = element.Attribute(XName.Get("val")).Value;

            foreach (T value2 in Enum.GetValues(typeof(T)))
            {
                FieldInfo field = typeof(T).GetField(value2.ToString());
                if (field.GetCustomAttributes(typeof(XmlNameAttribute), inherit: false).Count() == 0)
                {
                    throw new Exception($"Attribute 'XmlNameAttribute' is not assigned to {typeof(T).Name} fields!");
                }
                XmlNameAttribute xmlNameAttribute = (XmlNameAttribute)field.GetCustomAttributes(typeof(XmlNameAttribute), inherit: false).First();
                if (xmlNameAttribute.XmlName == value)
                {
                    return(value2);
                }
            }
            throw new ArgumentException("Invalid element value!");
        }
Exemplo n.º 4
0
        /// <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(nameof(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!");
        }