public static string ToDisplay <T, TProp>(Expression <Func <T, TProp> > propertyExpression, DisplayProperty property = DisplayProperty.Name)
        {
            var propInfo = propertyExpression.GetPropInfo();
            var attr     = propInfo.GetAttribute <DisplayNameAttribute>(false);

            return(attr.GetType().GetProperty(property.ToString()).GetValue(attr, null) as string ?? propInfo.Name);
        }
Exemple #2
0
        public static string EnumToDisplayName(this Enum value, DisplayProperty property = DisplayProperty.Name)
        {
            var attribute = value.GetType().GetField(value.ToString())
                            .GetCustomAttributes <DisplayAttribute>(false).FirstOrDefault();

            if (attribute == null)
            {
                return(value.ToString());
            }
            var propValue = attribute.GetType().GetProperty(property.ToString()).GetValue(attribute, null);

            return(propValue.ToString());
        }
Exemple #3
0
 public static string ToDisplay(this Enum value, DisplayProperty property = DisplayProperty.Name)
 {
     if (Assert.NotNull <Enum>(value))
     {
         object attribute = value.GetType().GetField(value.ToString()).GetCustomAttributes(false).FirstOrDefault();
         //CustomAttributeData customAttributeData = value.GetType().GetField(value.ToString()).CustomAttributes.FirstOrDefault(c => c.AttributeType == typeof(DisplayAttribute));
         if (Assert.NotNull <object>(attribute))
         {
             object propVal = attribute.GetType().GetProperty(property.ToString()).GetValue(attribute);
             //return customAttributeData.NamedArguments.FirstOrDefault(a => a.MemberName == property.ToString()).TypedValue.Value.ToString();
             return(propVal.ToString());
         }
     }
     return(value.ToString());
 }
Exemple #4
0
        public static string ToDisplay(this Enum value, DisplayProperty property = DisplayProperty.Name)
        {
            Assert.NotNull(value, nameof(value));
            List <string> Messages = new List <string>();

            var attribute = value.GetType().GetField(value.ToString())
                            .GetCustomAttributes <DisplayAttribute>(false).FirstOrDefault();

            if (attribute == null)
            {
                return(null);
            }

            return(attribute.GetType().GetProperty(property.ToString()).GetValue(attribute, null).ToString());
        }
Exemple #5
0
        public static string ToDisplay(this Enum value, DisplayProperty property = DisplayProperty.Name)
        {
            Assert.NotNull(value, nameof(value));

            var attribute = value.GetType().GetField(value.ToString())
                            .GetCustomAttributes(false).FirstOrDefault();

            if (attribute == null)
            {
                return(value.ToString());
            }

            var propValue = attribute.GetType().GetProperty(property.ToString()).GetValue(attribute, null);

            return(propValue.ToString());
        }
Exemple #6
0
        public static string ToDisplay(this Enum value, DisplayProperty property = DisplayProperty.Name)
        {
            if (value == null)
            {
                return(null);
            }
            var attributes =
                (DisplayAttribute[])
                value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(DisplayAttribute), false);

            if (attributes.Length > 0)
            {
                var attr      = attributes[0];
                var propValue = attr.GetType().GetProperty(property.ToString()).GetValue(attr, null);
                return(propValue as string);
            }
            return(value.ToString());
        }
Exemple #7
0
        /// <summary>
        /// Gets the display name.
        /// </summary>
        /// <typeparam name="TModel">The type of the model.</typeparam>
        /// <typeparam name="TField">The type of the field.</typeparam>
        /// <param name="selector">The selector.</param>
        /// <param name="involveAlnnotation">if set to <c>true</c> [involve alnnotation].</param>
        /// <param name="displayProperty">The display property.</param>
        /// <returns></returns>
        public static string GetDisplayName <TModel, TField>
            (Expression <Func <TModel, TField> > selector, bool involveAlnnotation = false,
            DisplayProperty displayProperty = DisplayProperty.Name)
        {
            try
            {
                System.Linq.Expressions.Expression body = selector;
                if (body is LambdaExpression)
                {
                    body = ((LambdaExpression)body).Body;
                }

                if (body.NodeType == ExpressionType.MemberAccess)
                {
                    PropertyInfo propInfo = (PropertyInfo)((MemberExpression)body).Member;
                    if (involveAlnnotation)
                    {
                        DisplayAttribute attribute = propInfo.GetCustomAttributes(typeof(DisplayAttribute), true)
                                                     .Select(prop => (DisplayAttribute)prop).FirstOrDefault();

                        if (attribute != null)
                        {
                            return(attribute.GetType().GetProperty(displayProperty.ToString())
                                   .GetValue(attribute, null).ToString());
                        }
                        else
                        {
                            return(propInfo.Name);
                        }
                    }
                    return(propInfo.Name);
                }
            }
            catch (Exception ex)
            {
                ex.ExceptionValueTracker(selector, involveAlnnotation);
            }
            return(string.Empty);
        }