/// <summary>
        /// 获取枚举指定的显示内容
        /// </summary>
        public static object Display(this Enum val, DisplayProperty property)
        {
            var enumType = val.GetType();
            //if val is Flag enum, each item will connect with ","
            var str = val.ToString();

            if (enumType.GetAttribute <FlagsAttribute>() != null && str.Contains(","))
            {
                var array = str.Split(",", StringSplitOptions.RemoveEmptyEntries).Select(o => o.Trim());

                var result = array.Aggregate("", (s, s1) =>
                {
                    var f = enumType.GetField(s1);

                    if (f != null)
                    {
                        var text = f.Display(property);
                        return(s.IsNullOrEmpty() ? text.ToString() : $"{s},{text}");
                    }

                    return(s);
                });

                return(result.IsNullOrEmpty() ? null : result);
            }

            var field = enumType.GetField(str);

            if (field != null)
            {
                return(field.Display(property));
            }

            return(null);
        }
Exemple #2
0
        /// <summary>
        /// 获取枚举指定的显示内容
        /// </summary>
        public static object Display(this MemberInfo memberInfo, DisplayProperty property)
        {
            if (memberInfo == null)
            {
                return(null);
            }

            var display = memberInfo.GetAttribute <DisplayAttribute>();

            if (display != null)
            {
                switch (property)
                {
                case DisplayProperty.Name:
                    return(display.GetName());

                case DisplayProperty.ShortName:
                    return(display.GetShortName());

                case DisplayProperty.GroupName:
                    return(display.GetGroupName());

                case DisplayProperty.Description:
                    return(display.GetDescription());

                case DisplayProperty.Order:
                    return(display.GetOrder());

                case DisplayProperty.Prompt:
                    return(display.GetPrompt());
                }
            }

            return(null);
        }
Exemple #3
0
 public override int GetHashCode()
 {
     unchecked
     {
         int hashCode = base.GetHashCode();
         hashCode = (hashCode * 397) ^ (Properties != null ? Properties.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ (IdProperty != null ? IdProperty.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ (DisplayProperty != null ? DisplayProperty.GetHashCode() : 0);
         return(hashCode);
     }
 }
Exemple #4
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());
        }
        /// <summary>
        /// 获取枚举指定的显示内容
        /// </summary>
        public static object Display(this Enum val, DisplayProperty property)
        {
            var enumType = val.GetType();
            var text     = val.ToString();
            var field    = enumType.GetField(text);

            if (field != null)
            {
                return(field.Display(property));
            }

            return(null);
        }
Exemple #6
0
        private static DisplayProperty parseCSS(string cssPath)
        {
            string cssContent = FileHelper.TryLoadFile(cssPath);
            var    parser     = new StylesheetParser();
            var    stylesheet = parser.Parse(cssContent);

            DisplayProperty displayProperty = new DisplayProperty();

            displayProperty.Title   = new DisplayStyle();
            displayProperty.Side    = new DisplayStyle();
            displayProperty.Display = new DisplayStyle();
            displayProperty.Tools   = new DisplayStyle();
            displayProperty.Search  = new DisplayStyle();
            displayProperty.Menu    = new DisplayStyle();
            displayProperty.Global  = new DisplayStyle();
            Type t1 = displayProperty.GetType();

            //在样式表中自上而下寻找
            foreach (StyleRule rule in stylesheet.StyleRules)
            {
                string selectors = rule.SelectorText;
                string color     = rule.Style.Color;
                string bg        = rule.Style.Background;
                string fontsize  = rule.Style.FontSize;
                int.TryParse(Regex.Match(fontsize, @"\d+").Value, out int fs);

                //获得选择器
                string[] selectTexts = selectors.Split('.');
                string   s1          = selectTexts[1].Trim();
                string   s2          = selectTexts[2].Trim();

                //反射
                DisplayStyle style = (DisplayStyle)t1.GetProperty(s1).GetValue(displayProperty);
                if (s2 == "Main")
                {
                    style.MainFontSize   = fs;
                    style.MainBackground = getColorFromRGBSring(bg);
                    style.MainForeground = getColorFromRGBSring(color);
                }
                else
                {
                    style.SubFontSize   = fs;
                    style.SubBackground = getColorFromRGBSring(bg);
                    style.SubForeground = getColorFromRGBSring(color);
                }
                t1.GetProperty(s1).SetValue(displayProperty, style);
            }

            return(displayProperty);
        }
Exemple #7
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 #8
0
        protected override void OnFormat(ListControlConvertEventArgs e)
        {
            // Display using the 'DisplayProperty' if specified
            if (DisplayProperty.HasValue())
            {
                m_disp_prop = m_disp_prop ?? e.ListItem.GetType().GetProperty(DisplayProperty, BindingFlags.Public | BindingFlags.Instance);
                e.Value     = m_disp_prop.GetValue(e.ListItem).ToString();
            }

            // Otherwise, fall back to the other methods
            else
            {
                base.OnFormat(e);
            }
        }
Exemple #9
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 #10
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 #11
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 #12
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);
        }
        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);
        }
        public void SetDisplayProperty(DisplayProperty property, byte value)
        {
            UsbCommand cmd = new UsbCommand((byte)property, value);

            PendingCommands.Add(cmd);
        }