Beispiel #1
0
        public virtual void Apply(StyleItemLine item)
        {
            MemberInfo mi = FindMember(item.TargetProperty);

            if (mi is FieldInfo fi)
            {
                object current = fi.GetValue(Target);
                if (TryParseValue(fi.FieldType, current, item, out object newValue))
                {
                    fi.SetValue(Target, newValue);
                }
            }
            else if (mi is PropertyInfo pi)
            {
                object current = pi.GetValue(Target);
                if (TryParseValue(pi.PropertyType, current, item, out object newValue))
                {
                    pi.SetValue(Target, newValue);
                }
            }
        }
Beispiel #2
0
        private bool TryParseValue(Type targetType, object currentValue, StyleItemLine item, out object result)
        {
            string value = item.Value;

            if (item.Value.StartsWith("(") && item.Value.EndsWith(")") && TryParseExpression(value, out string v))
            {
                value = v;
            }

            try
            {
                if (targetType == typeof(Color))
                {
                    if (objectCache.ContainsKey(value) && objectCache[value] is Color)
                    {
                        result = objectCache[value];
                    }
                    else
                    {
                        result             = ParseColor(value);
                        objectCache[value] = result;
                    }
                }

                if (typeof(Image).IsAssignableFrom(targetType))
                {
                    string path = Path.Combine(item.RootDir, value);
                    result = ImageCache.Load(path);
                }

                if (typeof(Font).IsAssignableFrom(targetType))
                {
                    result = FontCache.GetFont((Font)currentValue, value);
                }

                if (targetType.IsEnum)
                {
                    if (objectCache.ContainsKey(value) && objectCache[value].GetType() == targetType)
                    {
                        result = objectCache[value];
                    }
                    else
                    {
                        result             = Enum.Parse(targetType, value, true);
                        objectCache[value] = result;
                    }
                }

                if (objectCache.ContainsKey(value) && objectCache[value].GetType() == targetType)
                {
                    result = objectCache[value];
                }
                else
                {
                    result             = ParseLiteral(value, targetType);
                    objectCache[value] = result;
                }

                return(true);
            }
            catch (Exception)
            {
                result = null;
                return(false);
            }
        }