/// <summary>
        /// Gets the custom attribute defined for the specified enumeration member.
        /// </summary>
        /// <typeparam name="TAttribute">The type of the attribute.</typeparam>
        /// <param name="value">The enumeration value.</param>
        /// <returns>The defined attribute, or null.</returns>
        public static TAttribute GetAttribute <TAttribute>(this Enum value) where TAttribute : Attribute
        {
            var enumType  = value.GetType();
            var fieldInfo = enumType.GetField(Enum.GetName(enumType, value));

            return(XmlAttributeCache <TAttribute> .GetCustomAttribute(fieldInfo));
        }
Exemple #2
0
        /// <summary>
        /// Updates the name of the root element.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="type">The type.</param>
        /// <param name="removeTypePrefix">if set to <c>true</c> any type prefix will be removed.</param>
        /// <param name="elementNameOverride"></param>
        /// <returns>A new <see cref="XElement"/> instance.</returns>
        public static XElement UpdateRootElementName(this XElement element, Type type, bool removeTypePrefix = false, string elementNameOverride = null)
        {
            var xmlRoot = XmlAttributeCache <XmlRootAttribute> .GetCustomAttribute(type);

            var xmlType = XmlAttributeCache <XmlTypeAttribute> .GetCustomAttribute(type);

            var elementName = type.Name;

            if (!string.IsNullOrWhiteSpace(xmlRoot?.ElementName))
            {
                elementName = xmlRoot.ElementName;
            }

            else if (!string.IsNullOrWhiteSpace(xmlType?.TypeName))
            {
                elementName = xmlType.TypeName;
            }

            if (removeTypePrefix)
            {
                if (elementName.StartsWith("obj_"))
                {
                    elementName = elementName.Substring(4);
                }
                else if (elementName.StartsWith("cs_"))
                {
                    elementName = elementName.Substring(3);
                }
            }

            if (!string.IsNullOrWhiteSpace(elementNameOverride))
            {
                elementName = elementNameOverride;
            }

            if (element.Name.LocalName.Equals(elementName))
            {
                return(element);
            }

            var xElementName = !string.IsNullOrWhiteSpace(xmlRoot?.Namespace)
                ? XNamespace.Get(xmlRoot.Namespace).GetName(elementName)
                : !string.IsNullOrWhiteSpace(xmlType?.Namespace)
                    ? XNamespace.Get(xmlType.Namespace).GetName(elementName)
                    : elementName;

            // Update element name to match XSD type name
            var clone = new XElement(element)
            {
                Name = xElementName
            };

            // Remove xsi:type attribute used for abstract types
            clone.Attribute(Xsi.GetName("type"))?.Remove();

            return(clone);
        }
        /// <summary>
        /// Parses the enum.
        /// </summary>
        /// <param name="enumType">Type of the enum.</param>
        /// <param name="enumValue">The enum value.</param>
        /// <param name="ignoreCase">if set to <c>true</c> comparison is case insensitive.</param>
        /// <returns>The parsed enumeration value.</returns>
        public static object ParseEnum(this Type enumType, string enumValue, bool ignoreCase = true)
        {
            if (string.IsNullOrWhiteSpace(enumValue))
            {
                return(null);
            }

            enumType = Nullable.GetUnderlyingType(enumType) ?? enumType;

            try
            {
                double index;

                // Ensure enumValue is not numeric
#if DEBUG
                if (!double.TryParse(enumValue, out index) && Enum.IsDefined(enumType, enumValue))
#else
                if (!double.TryParse(enumValue, out index))
#endif
                { return(Enum.Parse(enumType, enumValue, ignoreCase)); }
            }
            catch
            {
                // Ignore
            }

            var mode = ignoreCase
                ? StringComparison.InvariantCultureIgnoreCase
                : StringComparison.InvariantCulture;

            var enumMember = enumType.GetMembers().FirstOrDefault(x =>
            {
                if (string.Equals(x.Name, enumValue, mode))
                {
                    return(true);
                }

                var xmlEnumAttrib = XmlAttributeCache <XmlEnumAttribute> .GetCustomAttribute(x);
                if (xmlEnumAttrib != null && string.Equals(xmlEnumAttrib.Name, enumValue, mode))
                {
                    return(true);
                }

                var descriptionAttr = XmlAttributeCache <DescriptionAttribute> .GetCustomAttribute(x);
                return(descriptionAttr != null && string.Equals(descriptionAttr.Description, enumValue, mode));
            });

            // must be a valid enumeration member
            if (!enumType.IsEnum || enumMember == null)
            {
                throw new ArgumentException();
            }

            return(Enum.Parse(enumType, enumMember.Name, ignoreCase));
        }
            public EnumMemberInfo(Type enumType)
            {
                // must be a valid enumeration member
                if (!enumType.IsEnum)
                {
                    throw new ArgumentException($"{enumType} must be a valid enumeration type");
                }

                FieldInfo    = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);
                MappedValues = new Dictionary <long, object>();

                for (var i = 0; i < FieldInfo.Length; i++)
                {
                    var field            = FieldInfo[i];
                    var fieldName        = field.Name;
                    var fieldValue       = field.GetValue(null);
                    var fieldConstant    = Convert.ToInt64(field.GetRawConstantValue());
                    var xmlEnumAttribute = XmlAttributeCache <XmlEnumAttribute> .GetCustomAttribute(field);

                    var descriptionAttribute = XmlAttributeCache <DescriptionAttribute> .GetCustomAttribute(field);

                    MappedValues[fieldConstant] = fieldValue;

                    EnumMemberMappings.Item1.NamedValues[fieldName] = fieldValue;
                    EnumMemberMappings.Item2.NamedValues[fieldName] = fieldValue;

                    if (xmlEnumAttribute != null)
                    {
                        EnumMemberMappings.Item1.EnumAttributeValues[xmlEnumAttribute.Name] = fieldValue;
                        EnumMemberMappings.Item2.EnumAttributeValues[xmlEnumAttribute.Name] = fieldValue;
                    }

                    if (descriptionAttribute != null)
                    {
                        EnumMemberMappings.Item1.DescriptionAttributeValues[descriptionAttribute.Description] = fieldValue;
                        EnumMemberMappings.Item2.DescriptionAttributeValues[descriptionAttribute.Description] = fieldValue;
                    }
                }
            }
Exemple #5
0
        /// <summary>
        /// Updates the name of the root element.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="type">The type.</param>
        /// <returns>A new <see cref="XElement"/> instance.</returns>
        public static XElement UpdateRootElementName(this XElement element, Type type)
        {
            var xmlRoot = XmlAttributeCache <XmlRootAttribute> .GetCustomAttribute(type);

            var xmlType = XmlAttributeCache <XmlTypeAttribute> .GetCustomAttribute(type);

            var elementName = type.Name;

            if (!string.IsNullOrWhiteSpace(xmlRoot?.ElementName))
            {
                elementName = xmlRoot.ElementName;
            }

            else if (!string.IsNullOrWhiteSpace(xmlType?.TypeName))
            {
                elementName = xmlType.TypeName;
            }

            if (element.Name.LocalName.Equals(elementName))
            {
                return(element);
            }

            var xElementName = !string.IsNullOrWhiteSpace(xmlRoot?.Namespace)
                ? XNamespace.Get(xmlRoot.Namespace).GetName(elementName)
                : elementName;

            // Update element name to match XSD type name
            var clone = new XElement(element)
            {
                Name = xElementName
            };

            // Remove xsi:type attribute used for abstract types
            clone.Attribute(Xsi.GetName("type"))?.Remove();

            return(clone);
        }
Exemple #6
0
        /// <summary>
        /// Parses the enum.
        /// </summary>
        /// <param name="enumType">Type of the enum.</param>
        /// <param name="enumValue">The enum value.</param>
        /// <returns></returns>
        public static object ParseEnum(this Type enumType, string enumValue)
        {
            if (string.IsNullOrWhiteSpace(enumValue))
            {
                return(null);
            }

            if (Enum.IsDefined(enumType, enumValue))
            {
                return(Enum.Parse(enumType, enumValue));
            }

            var enumMember = enumType.GetMembers().FirstOrDefault(x =>
            {
                if (x.Name.EqualsIgnoreCase(enumValue))
                {
                    return(true);
                }

                var xmlEnumAttrib = XmlAttributeCache <XmlEnumAttribute> .GetCustomAttribute(x);
                if (xmlEnumAttrib != null && xmlEnumAttrib.Name.EqualsIgnoreCase(enumValue))
                {
                    return(true);
                }

                var descriptionAttr = XmlAttributeCache <DescriptionAttribute> .GetCustomAttribute(x);
                return(descriptionAttr != null && descriptionAttr.Description.EqualsIgnoreCase(enumValue));
            });

            // must be a valid enumeration member
            if (!enumType.IsEnum || enumMember == null)
            {
                throw new ArgumentException();
            }

            return(Enum.Parse(enumType, enumMember.Name));
        }