Beispiel #1
0
        public object ReadExternalizable(string key)
        {
            object value;

            if (_values.TryGetValue(key, out value))
            {
                return(_context.GetExternalizable(key, value as IMemento));
            }
            else
            {
                return(null);
            }
        }
        public virtual void ReadExternal(IMemento memento, ExternalizeContext context)
        {
            _isVisible = memento.ReadBool("IsVisible");
            _layout    = memento.ReadExternalizable("Layout") as ILayout;
            if (_layout != null)
            {
                _layout.Owner = this;
            }
            if (memento.Contains("LayoutConstraints"))
            {
                var constraints = memento.ReadSerializable("LayoutConstraints") as Dictionary <IMemento, object>;
                if (constraints.Count > 0)
                {
                    foreach (var pair in constraints)
                    {
                        var child = context.GetExternalizable("Children", pair.Key) as IFigure;
                        LayoutConstraints[child] = pair.Value;
                    }
                }
            }
            if (memento.Contains("PersistentData"))
            {
                var data = memento.ReadSerializable("PersistentData") as Dictionary <string, object>;
                if (data.Count > 0)
                {
                    foreach (var pair in data)
                    {
                        PersistentData[pair.Key] = pair.Value;
                    }
                }
            }

            if (memento.Contains("Children"))
            {
                var children = memento.ReadExternalizables("Children");
                foreach (var child in children)
                {
                    var childFig = child as IFigure;
                    if (childFig != null)
                    {
                        Children.Add(childFig);
                    }
                }
            }
        }
Beispiel #3
0
        public override void ReadExternal(IMemento memento, ExternalizeContext context)
        {
            base.ReadExternal(memento, context);

            /// ValueにFigureのMementoが入ったTableDataから復元する
            var memTableData = memento.ReadSerializable("TableData") as TableData <IMemento>;

            _tableData.Redim(memTableData.RowCount, memTableData.ColumnCount);

            _tableData.MinRowHeight   = memTableData.MinRowHeight;
            _tableData.MinColumnWidth = memTableData.MinColumnWidth;

            /// row
            for (int i = 0, len = _tableData.RowCount; i < len; ++i)
            {
                var row    = _tableData.Rows.ElementAt(i);
                var memRow = memTableData.Rows.ElementAt(i);
                row.Height = memRow.Height;
            }

            /// column
            for (int i = 0, len = _tableData.ColumnCount; i < len; ++i)
            {
                var col    = _tableData.Columns.ElementAt(i);
                var memCol = memTableData.Columns.ElementAt(i);
                col.Width = memCol.Width;
            }
            /// cell
            for (int i = 0, len = _tableData.CellCount; i < len; ++i)
            {
                var cell    = _tableData.Cells.ElementAt(i);
                var memCell = memTableData.Cells.ElementAt(i);
                cell.Value = context.GetExternalizable("Cell", memCell.Value) as TableCellFigure;

                //cell.ColumnSpan = memCell.ColumnSpan;
                //cell.RowSpan = memCell.RowSpan;
                if (memCell.ColumnSpan != 1 || memCell.RowSpan != 1)
                {
                    _tableData.SetSpan(cell, memCell.ColumnSpan, memCell.RowSpan);
                }
                cell.Color = memCell.Color;
            }
        }
Beispiel #4
0
        public void LoadFrom(IMemento memento, ExternalizeContext context)
        {
            var type = _target.GetType();

            foreach (var prop in _properties)
            {
                var externalAttr = TypeService.Instance.GetExternalAttribute(prop);
                if (externalAttr == null)
                {
                    continue;
                }

                var propName = prop.Name;
                var value    = memento.ReadSerializable(propName);
                if (value == null)
                {
                    continue;
                }

                var valueType = value.GetType();
                var propType  = prop.PropertyType;

                if (GenericTypeUtil.IsGenericIDictionary(propType))
                {
                    var elemTypes     = GenericTypeUtil.GetGenericArgumentOfGenericIDictionary(valueType);
                    var dictKeyType   = elemTypes[0];
                    var dictValueType = elemTypes[1];

                    var setItemMethod = propType.GetMethod("set_Item");

                    if (typeof(IMemento).IsAssignableFrom(dictKeyType))
                    {
                        if (typeof(IMemento).IsAssignableFrom(dictValueType))
                        {
                            var dict = memento.ReadSerializable(propName) as IDictionary <IMemento, IMemento>;
                            foreach (var pair in dict)
                            {
                                var keyEx = context.GetExternalizable(propName, pair.Key);
                                var valEx = context.GetExternalizable(propName, pair.Value);
                                if (keyEx != null && valEx != null)
                                {
                                    setItemMethod.Invoke(value, new[] { keyEx, valEx });
                                }
                            }
                        }
                        else
                        {
                            var dict = memento.ReadSerializable(propName) as IDictionary <IMemento, object>;
                            foreach (var pair in dict)
                            {
                                var keyEx = context.GetExternalizable(propName, pair.Key);
                                if (keyEx != null)
                                {
                                    setItemMethod.Invoke(value, new[] { keyEx, pair.Value });
                                }
                            }
                        }
                    }
                    else
                    {
                        if (typeof(IMemento).IsAssignableFrom(dictValueType))
                        {
                            var dict = memento.ReadSerializable(propName) as IDictionary <object, IMemento>;
                            foreach (var pair in dict)
                            {
                                var valEx = context.GetExternalizable(propName, pair.Value);
                                if (valEx != null)
                                {
                                    setItemMethod.Invoke(value, new[] { pair.Key, valEx });
                                }
                            }
                        }
                        else
                        {
                            var dict = memento.ReadSerializable(propName) as IDictionary <object, IMemento>;
                            foreach (var pair in dict)
                            {
                                setItemMethod.Invoke(value, new[] { pair.Key, pair.Value, });
                            }
                        }
                    }
                }
                else if (GenericTypeUtil.IsGenericICollection(propType))
                {
                    var elemType = GenericTypeUtil.GetGenericArgumentOfGenericICollection(valueType);

                    if (typeof(IMemento).IsAssignableFrom(elemType))
                    {
                        var add       = externalAttr.Add;
                        var addMethod = add != null?type.GetMethod(add) : propType.GetMethod("Add");

                        var list            = prop.GetValue(_target, null);
                        var externalizables = memento.ReadExternalizables(propName);
                        foreach (var ex in externalizables)
                        {
                            addMethod.Invoke(list, new[] { ex });
                        }
                    }
                    else
                    {
                        var add       = externalAttr.Add;
                        var addMethod = add != null?type.GetMethod(add) : propType.GetMethod("Add");

                        var list         = prop.GetValue(_target, null);
                        var deserialized = memento.ReadSerializable(propName) as IEnumerable;
                        foreach (var ex in deserialized)
                        {
                            addMethod.Invoke(list, new[] { ex });
                        }
                    }
                }
                else if (GenericTypeUtil.IsGenericIEnumerable(propType) && externalAttr.Add != null)
                {
                    var elemType = GenericTypeUtil.GetGenericArgumentOfGenericIEnumerable(valueType);

                    if (typeof(IMemento).IsAssignableFrom(elemType))
                    {
                        var add             = externalAttr.Add;
                        var addMethod       = type.GetMethod(add);
                        var externalizables = memento.ReadExternalizables(propName);
                        foreach (var ex in externalizables)
                        {
                            addMethod.Invoke(_target, new[] { ex });
                        }
                    }
                    else
                    {
                        var add          = externalAttr.Add;
                        var addMethod    = type.GetMethod(add);
                        var deserialized = memento.ReadSerializable(propName) as IEnumerable;
                        foreach (var ex in deserialized)
                        {
                            addMethod.Invoke(_target, new[] { ex });
                        }
                    }
                }
                else
                {
                    if (valueType.IsPrimitive)
                    {
                        prop.SetValue(_target, memento.ReadSerializable(propName), null);
                    }
                    else
                    {
                        if (typeof(IMemento).IsAssignableFrom(valueType))
                        {
                            prop.SetValue(_target, memento.ReadExternalizable(propName), null);
                        }
                        else
                        {
                            prop.SetValue(_target, memento.ReadSerializable(propName), null);
                        }
                    }
                }
            }

            var externalizableAttr = TypeService.Instance.GetExternalizableAttribute(_type);

            if (externalizableAttr.Loaded != null)
            {
                var loaded = _type.GetMethod(externalizableAttr.Loaded);
                loaded.Invoke(_target, new object[] { memento, context });
            }
        }