示例#1
0
        public static CSSStyleDeclaration Parse(Dictionary <string, string> fields)
        {
            var style       = new CSSStyleDeclaration();
            var styleType   = style.GetType();
            var styleFields = style.ExtractAllFields();

            foreach (var keyValuePair in fields)
            {
                var fieldName = keyValuePair.Key.Replace("\r", "");
                var value     = keyValuePair.Value;

                if (styleFields.ContainsKey(fieldName))
                {
                    var refType     = styleFields[fieldName];
                    var fieldType   = refType.PropertyType;
                    var parseMethod = fieldType.GetMethod("Parse");

                    var property = styleType.GetProperty(refType.PropertyPath.FirstOrDefault());
                    var propertyParentInstance = (object)style;

                    style.ChangedFields.Add(property.Name);

                    for (var i = 1; i < refType.PropertyPath.Count; i++)
                    {
                        var pathToField = refType.PropertyPath[i];

                        propertyParentInstance = property.GetValue(propertyParentInstance, null);
                        property = property.PropertyType.GetProperty(pathToField);
                    }

                    if (value.FirstOrDefault() == ' ')
                    {
                        value = value.Substring(1, value.Length - 1);
                    }

                    var result = parseMethod.Invoke(null, new [] { value });

                    property.SetValue(propertyParentInstance, result);
                }
            }

            return(style);
        }
示例#2
0
        public object Clone()
        {
            var style = new CSSStyleDeclaration
            {
                Color      = this.Color.Clone() as CSSColor,
                Background = Background.Clone() as CSSBackground,
                Padding    = Padding.Clone() as CSSPadding,
                Margin     = Margin.Clone() as CSSMargin,
                Border     = Border.Clone() as CSSBorder,
                Cursor     = Cursor.Clone() as CSSCursor,
                Float      = Float.Clone() as CSSFloat,
                Font       = Font.Clone() as CSSFont,
                Left       = Left.Clone() as CSSNumber,
                Right      = Right.Clone() as CSSNumber,
                Width      = Width.Clone() as CSSNumber,
                Height     = Height.Clone() as CSSNumber,
                Display    = Display.Clone() as CSSDisplay,
                Position   = Position.Clone() as CSSPosition
            };

            return(style);
        }