Exemple #1
0
        // ========================================
        // method
        // ========================================
        // === IPersistable ==========
        public override void WriteExternal(IMemento memento, ExternalizeContext context)
        {
            base.WriteExternal(memento, context);

            memento.WriteExternalizable("SourceDecoration", _sourceDecoration);
            memento.WriteExternalizable("TargetDecoration", _targetDecoration);
        }
        // ========================================
        // 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>());
            }
        }
Exemple #3
0
 public static void SaveFoo(Foo foo, IMemento mem, ExternalizeContext context)
 {
     mem.WriteInt("Value", foo.Value);
     mem.WriteExternalizable(
         "Bar",
         new BarProxy(foo.Bar, SaveBar, LoadBar)
         );
 }
        // === IPersistable ==========
        public void WriteExternal(IMemento memento, ExternalizeContext context)
        {
            var modelSerializer = context.ExtendedData[EditorConsts.ModelSerializerKey] as IModelSerializer;

            if (modelSerializer == null)
            {
                throw new ArgumentException("context.ExtendedData is invalid");
            }

            memento.WriteExternalizable("Figure", Figure);
            memento.WriteSerializable("ModelHint", modelSerializer.Save(Model));
            memento.WriteExternalizables("Children", Children.As <Editor, object>());
        }
Exemple #5
0
        // ========================================
        // method
        // ========================================
        // === IPersistable ==========
        public override void WriteExternal(IMemento memento, ExternalizeContext context)
        {
            base.WriteExternal(memento, context);

            memento.WriteExternalizable("Router", _router);

            memento.WriteSerializable("EdgePoints", _edgePoints.ToArray());
            memento.WriteInt("HitMargin", _hitMargin);
            memento.WriteSerializable("LineColor", _lineColor);
            memento.WriteInt("LineWidth", _lineWidth);
            memento.WriteSerializable("DashStyle", _lineDashStyle);
            memento.WriteSerializable("ConnectionMethod", _connectionMethod);

            memento.WriteSerializable("SourceConnectionOption", _sourceConnectionOption);
            memento.WriteSerializable("TargetConnectionOption", _targetConnectionOption);

            memento.WriteSerializable("EdgeBehaviorOptions", _behaviorOptions);
        }
 public virtual void WriteExternal(IMemento memento, ExternalizeContext context)
 {
     memento.WriteInt("Value", _value);
     memento.WriteExternalizable("Foo", _foo);
 }
Exemple #7
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 });
            }
        }