Exemplo n.º 1
0
        private static string GetEnumDisplay <T>(T enumObj, EnumProp enumProp, FieldInfo fieldInfo)
        {
            var attribArray = GetCustomAttributes(fieldInfo, enumProp);

            return(attribArray.Length == 0
                ? enumObj.ToString()
                : GetEnumDisplay(enumProp, attribArray));
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Retrieves the display name (specified in the <see cref="EnumDisplayNameAttribute" />
        ///     custom attribute) for the passed in <see cref="System.Enum" /> instance.
        /// </summary>
        /// <returns>the display name for the specified enum</returns>
        /// <remarks>
        ///     The enum specified must implement the <see cref="EnumDisplayNameAttribute" />
        ///     custom attribute.  If it does not, an empty string is returned
        /// </remarks>
        /// <param name="enumeratedValue"></param>
        /// <param name="enumProp"></param>
        /// <returns></returns>
        public static string GetEnumDisplayName(System.Enum enumeratedValue, EnumProp enumProp)
        {
            var fieldInfo   = enumeratedValue.GetType().GetField(enumeratedValue.ToString());
            var attribArray = GetCustomAttributes(fieldInfo, enumProp);

            return(attribArray.Length == 0
                ? string.Empty
                : GetEnumDisplay(enumProp, attribArray));
        }
Exemplo n.º 3
0
        /// <summary>
        ///     Retrieves the display name (specified in the <see cref="EnumDisplayNameAttribute" />
        ///     custom attribute) for the passed in <see cref="System.Enum" /> instance.
        /// </summary>
        /// <param name="enumObj">an Enum whose displayName is being requested</param>
        /// <param name="enumProp">and Enum property for display type </param>
        /// <returns>the display name for the specified enum</returns>
        /// <remarks>
        ///     The enum specified must implement the <see cref="EnumDisplayNameAttribute" />
        ///     custom attribute.  If it does not, an empty string is returned
        /// </remarks>
        public static string GetEnumDisplayValue <T>(T enumObj, EnumProp enumProp)
        {
            if (enumObj is System.Enum == false)
            {
                throw new ArgumentException("The specified generic type must derive from System.Enum");
            }
            var fieldInfo = enumObj.GetType().GetField(enumObj.ToString());

            if (fieldInfo == null)
            {
                return(string.Empty);
            }

            return(GetEnumDisplay(enumObj, enumProp, fieldInfo));
        }
Exemplo n.º 4
0
 public override int GetHashCode()
 {
     unchecked
     {
         int result = (StringProp != null ? StringProp.GetHashCode() : 0);
         result = (result * 397) ^ IntProp;
         result = (result * 397) ^ DoubleProp.GetHashCode();
         result = (result * 397) ^ BoolProp.GetHashCode();
         result = (result * 397) ^ (ReadOnlyProp != null ? ReadOnlyProp.GetHashCode() : 0);
         result = (result * 397) ^ (WriteOnlyProp != null ? WriteOnlyProp.GetHashCode() : 0);
         result = (result * 397) ^ (IgnoreProp != null ? IgnoreProp.GetHashCode() : 0);
         result = (result * 397) ^ EnumProp.GetHashCode();
         result = (result * 397) ^ MappedProp;
         result = (result * 397) ^ Field?.GetHashCode() ?? 0;
         return(result);
     }
 }
Exemplo n.º 5
0
        /// <summary>
        ///     Gets the field of the specified enum type who's displayName matches the
        ///     specified enumDisplayValue
        /// </summary>
        /// <typeparam name="T">
        ///     the enum type who's field's are decorated with the
        ///     <see cref="EnumDisplayNameAttribute" />
        /// </typeparam>
        /// <param name="enumDisplayValue">the displayName to search for</param>
        /// <param name="enumProp"></param>
        /// <param name="throwExceptionIfNotFound">
        ///     flag indicating whether or not
        ///     to throw an exception if no field is found with a displayName that matches
        ///     the specified displayName
        /// </param>
        /// <returns>a field of the specified enum Type (T)</returns>
        public static T GetEnumValue <T>(string enumDisplayValue, EnumProp enumProp, bool throwExceptionIfNotFound)
        {
            var retVal = default(T);
            var found  = false;

            if (enumDisplayValue != null)
            {
                foreach (var fieldInfo in typeof(T).GetFields())
                {
                    if (fieldInfo.FieldType != typeof(T))
                    {
                        continue;
                    }
                    var attribArray = enumProp == EnumProp.DisplayName
                        ? fieldInfo.GetCustomAttributes(typeof(EnumDisplayNameAttribute), false)
                        : fieldInfo.GetCustomAttributes(typeof(EnumDisplayCodeAttribute), false);
                    switch (attribArray.Length)
                    {
                    case 0:
                        continue;

                    default:
                        if (enumDisplayValue.ToUpper() ==
                            (enumProp == EnumProp.DisplayName
                                    ? ((EnumDisplayNameAttribute)attribArray[0]).DisplayName.ToUpper()
                                    : ((EnumDisplayCodeAttribute)attribArray[0]).DisplayCode.ToUpper()))
                        {
                            found  = true;
                            retVal = (T)fieldInfo.GetValue(fieldInfo);
                        }
                        break;
                    }
                }
            }

            if (!found && throwExceptionIfNotFound)
            {
                throw new ArgumentException(string.Format("No value of Enum {0} has a display name of {1}",
                                                          typeof(T).Name, enumDisplayValue));
            }

            return(retVal);
        }
Exemplo n.º 6
0
        private static string GetEnumDisplay(EnumProp enumProp, object[] attribArray)
        {
            switch (enumProp)
            {
            case EnumProp.DisplayName:
            {
                var attrib = attribArray[0] as EnumDisplayNameAttribute;
                return(attrib != null ? attrib.DisplayName : string.Empty);
            }

            case EnumProp.DisplayCode:
            {
                var attrib = attribArray[0] as EnumDisplayCodeAttribute;
                return(attrib != null ? attrib.DisplayCode : string.Empty);
            }

            default:
                return(string.Empty);
            }
        }
Exemplo n.º 7
0
 private static object[] GetCustomAttributes(FieldInfo fieldInfo, EnumProp enumProp)
 {
     return(enumProp == EnumProp.DisplayName
         ? fieldInfo.GetCustomAttributes(typeof(EnumDisplayNameAttribute), false)
         : fieldInfo.GetCustomAttributes(typeof(EnumDisplayCodeAttribute), false));
 }
Exemplo n.º 8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="lst"></param>
        /// <param name="displayCode"></param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public static string GetEnumDisplayName <T>(string lst, EnumProp displayCode)
        {
            var enumratedValue = lst.ToEnum <T>();

            return(GetEnumDisplayName(enumratedValue as System.Enum, displayCode));
        }