// ========================================
        // method
        // ========================================
        public virtual void WriteExternal(IMemento memento, ExternalizeContext context)
        {
            memento.WriteBool("IsVisible", _isVisible);
            memento.WriteExternalizable("Layout", _layout);

            if (_layoutConstraints != null)
            {
                var constraints = new Dictionary <IMemento, object>();
                foreach (var pair in _layoutConstraints)
                {
                    var childMem = context.GetMemento("Children", pair.Key);
                    if (childMem != null)
                    {
                        constraints[childMem] = pair.Value;
                    }
                }
                memento.WriteSerializable("LayoutConstraints", constraints);
            }

            var data = new Dictionary <string, object>();

            if (_persistentData != null)
            {
                foreach (var pair in _persistentData)
                {
                    data[pair.Key] = pair.Value;
                }
            }
            memento.WriteSerializable("PersistentData", data);

            if (_structure.HasChildren)
            {
                memento.WriteExternalizables("Children", Children.As <IFigure, object>());
            }
        }
Ejemplo n.º 2
0
        // ========================================
        // method
        // ========================================
        // === IPersistable ==========
        public override void WriteExternal(IMemento memento, ExternalizeContext context)
        {
            base.WriteExternal(memento, context);

            /// ValueにFigureのMementoを入れたTableDataを作る
            var memTableData = new TableData <IMemento>();

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

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

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

            /// column
            for (int i = 0, len = _tableData.ColumnCount; i < len; ++i)
            {
                var col    = _tableData.Columns.ElementAt(i);
                var memCol = memTableData.Columns.ElementAt(i);
                memCol.Width = col.Width;
            }

            /// cell
            for (int i = 0, len = _tableData.CellCount; i < len; ++i)
            {
                var cell    = _tableData.Cells.ElementAt(i);
                var memCell = memTableData.Cells.ElementAt(i);
                memCell.Value = context.GetMemento("Cell", cell.Value);
                //memCell.ColumnSpan = cell.ColumnSpan;
                //memCell.RowSpan = cell.RowSpan;
                if (cell.ColumnSpan != 1 || cell.RowSpan != 1)
                {
                    memTableData.SetSpan(memCell, cell.ColumnSpan, cell.RowSpan);
                }
                memCell.Color = cell.Color;
            }

            memento.WriteSerializable("TableData", memTableData);
        }
Ejemplo n.º 3
0
        // ========================================
        // property
        // ========================================

        // ========================================
        // method
        // ========================================
        public void SaveTo(IMemento memento, ExternalizeContext context)
        {
            foreach (var prop in _properties)
            {
                var externalAttr = TypeService.Instance.GetExternalAttribute(prop);
                if (externalAttr == null)
                {
                    continue;
                }

                var value = prop.GetValue(_target, null);
                if (value == null)
                {
                    continue;
                }

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


                if (GenericTypeUtil.IsGenericIDictionary(propType))
                {
                    var elemTypes             = GenericTypeUtil.GetGenericArgumentOfGenericIDictionary(valueType);
                    var isKeyExternalizable   = IsExternalizable(elemTypes[0]);
                    var isValueExternalizable = IsExternalizable(elemTypes[1]);

                    var dictType      = typeof(IDictionary <,>).MakeGenericType(elemTypes);
                    var getKeysMethod = dictType.GetMethod("get_Keys");
                    var getItemMethod = dictType.GetMethod("get_Item");

                    if (isKeyExternalizable)
                    {
                        if (isValueExternalizable)
                        {
                            var dict = new Dictionary <IMemento, IMemento>();
                            var keys = getKeysMethod.Invoke(value, null) as ICollection;
                            foreach (var key in keys)
                            {
                                var entryValue = getItemMethod.Invoke(value, new[] { key });
                                var keyMem     = context.GetMemento(propName, key);
                                var valMem     = context.GetMemento(propName, entryValue);
                                if (keyMem != null && valMem != null)
                                {
                                    dict.Add(keyMem, valMem);
                                }
                            }
                            memento.WriteSerializable(propName, dict);
                        }
                        else
                        {
                            var dict = new Dictionary <IMemento, object>();
                            var keys = getKeysMethod.Invoke(value, null) as ICollection;
                            foreach (var key in keys)
                            {
                                var entryValue = getItemMethod.Invoke(value, new[] { key });
                                var keyMem     = context.GetMemento(propName, key);
                                if (keyMem != null)
                                {
                                    dict.Add(keyMem, entryValue);
                                }
                            }
                            memento.WriteSerializable(propName, dict);
                        }
                    }
                    else
                    {
                        if (isValueExternalizable)
                        {
                            var dict = new Dictionary <object, IMemento>();
                            var keys = getKeysMethod.Invoke(value, null) as ICollection;
                            foreach (var key in keys)
                            {
                                var entryValue = getItemMethod.Invoke(value, new[] { key });
                                var valMem     = context.GetMemento(propName, entryValue);
                                if (valMem != null)
                                {
                                    dict.Add(key, valMem);
                                }
                            }
                            memento.WriteSerializable(propName, dict);
                        }
                        else
                        {
                            var dict = new Dictionary <object, object>();
                            var keys = getKeysMethod.Invoke(value, null) as ICollection;
                            foreach (var key in keys)
                            {
                                var entryValue = getItemMethod.Invoke(value, new[] { key });
                                dict.Add(key, entryValue);
                            }
                            memento.WriteSerializable(propName, dict);
                        }
                    }
                }
                else if (GenericTypeUtil.IsGenericICollection(propType))
                {
                    var elemType             = GenericTypeUtil.GetGenericArgumentOfGenericICollection(valueType);
                    var isElemExternalizable = IsExternalizable(elemType);

                    if (isElemExternalizable)
                    {
                        var list = new List <IMemento>();
                        foreach (var v in value as IEnumerable)
                        {
                            var elem = context.GetMemento(propName, v);
                            if (elem != null)
                            {
                                list.Add(elem);
                            }
                        }
                        memento.WriteSerializable(propName, list);
                    }
                    else
                    {
                        var list = new List <object>();
                        foreach (var v in value as IEnumerable)
                        {
                            list.Add(v);
                        }
                        memento.WriteSerializable(propName, list);
                    }
                }
                else if (GenericTypeUtil.IsGenericIEnumerable(propType) && externalAttr.Add != null)
                {
                    var elemType             = GenericTypeUtil.GetGenericArgumentOfGenericIEnumerable(valueType);
                    var isElemExternalizable = IsExternalizable(elemType);

                    if (isElemExternalizable)
                    {
                        var list = new List <IMemento>();
                        foreach (var v in value as IEnumerable)
                        {
                            var elem = context.GetMemento(propName, v);
                            if (elem != null)
                            {
                                list.Add(elem);
                            }
                        }
                        memento.WriteSerializable(propName, list);
                    }
                    else
                    {
                        var list = new List <object>();
                        foreach (var v in value as IEnumerable)
                        {
                            list.Add(v);
                        }
                        memento.WriteSerializable(propName, list);
                    }
                }
                else
                {
                    if (valueType.IsPrimitive)
                    {
                        memento.WriteSerializable(propName, value);
                    }
                    else
                    {
                        if (IsExternalizable(valueType))
                        {
                            memento.WriteExternalizable(propName, value);
                        }
                        else
                        {
                            if (externalAttr.Clone == null)
                            {
                                memento.WriteSerializable(propName, value);
                            }
                            else
                            {
                                try {
                                    var flags =
                                        BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
                                    var clone  = _type.GetMethod(externalAttr.Clone, flags);
                                    var cloned = clone.Invoke(_target, null);
                                    memento.WriteSerializable(propName, cloned);
                                } catch (Exception e) {
                                    Logger.Error(
                                        string.Format(
                                            "Failed to invoke {0}.{1} for clone",
                                            _type.Name, externalAttr.Clone
                                            ),
                                        e
                                        );
                                    throw;
                                }
                            }
                        }
                    }
                }
            }

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

            if (externalizableAttr.FactoryMethodType != null && externalizableAttr.FactoryMethod != null)
            {
                var factoryMethod = externalizableAttr.FactoryMethodType.GetMethod(
                    externalizableAttr.FactoryMethod,
                    BindingFlags.Public | BindingFlags.Static
                    );
                if (factoryMethod != null)
                {
                    memento.FactoryMethod = factoryMethod;

                    var paras = externalizableAttr.FactoryMethodParamKeys;
                    if (paras != null)
                    {
                        memento.FactoryMethodParamKeys = paras;
                    }
                }
            }

            if (externalizableAttr.ConstructorParamKeys != null)
            {
                memento.ConstructorParamKeys = externalizableAttr.ConstructorParamKeys;
            }

            if (externalizableAttr.Saved != null)
            {
                var saved = _type.GetMethod(externalizableAttr.Saved);
                saved.Invoke(_target, new object[] { memento, context });
            }
        }
Ejemplo n.º 4
0
 public void WriteExternalizable(string key, object value)
 {
     _values[key] = _context.GetMemento(key, value);
 }