Exemple #1
0
        /// <summary>取得DisplayAttribute公開屬性內容</summary>
        /// <param name="attr">DisplayAttribute</param>
        /// <param name="propName">公開屬性名稱,預設Name</param>
        /// <returns>The value that is used for display in the UI</returns>
        public static string GetDisplay(this DisplayAttribute attr, string propName = "Name")
        {
            string       empty    = string.Empty;
            PropertyInfo property = attr.GetType().GetProperty(propName);

            if (property != null)
            {
                empty = (string)property.GetValue(attr);
            }

            return(empty);
        }
Exemple #2
0
        public static string ToDisplay(this Enum value, DisplayProperty property = DisplayProperty.Name)
        {
            if (value == null)
            {
                return(null);
            }

            DisplayAttribute[] attributes =
                (DisplayAttribute[])
                value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(DisplayAttribute), false);
            if (attributes.Length > 0)
            {
                DisplayAttribute attr      = attributes[0];
                object           propValue = attr.GetType().GetProperty(property.ToString()).GetValue(attr, null);
                return(propValue as string);
            }
            return(value.ToString());
        }
 private static string GetDisplayAttributeName(Type containerType, string propertyName,
                                               DisplayAttribute displayAttribute)
 {
     if (containerType != null)
     {
         if (String.IsNullOrEmpty(displayAttribute.Name))
         {
             // check to see that resource key exists.
             string attributeShortName = displayAttribute.GetType().Name.Replace("Attribute", "");
             string resourceKey        = propertyName + "_" + attributeShortName;
             if (displayAttribute.ResourceType.PropertyExists(resourceKey))
             {
                 return(resourceKey);
             }
             return(null);
         }
     }
     return(null);
 }
Exemple #4
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);
        }