Ejemplo n.º 1
0
            private object DeserializeEntry(IDesignerSerializationManager manager, ObjectEntry objectEntry)
            {
                object deserialized = null;

                if (objectEntry.IsEntireObject)
                {
                    CodeDomSerializer serializer = (CodeDomSerializer)manager.GetSerializer(objectEntry.Type,
                                                                                            typeof(CodeDomSerializer));
                    if (serializer != null)
                    {
                        deserialized = serializer.Deserialize(manager, objectEntry.Serialized);
                        // check if the name of the object has changed
                        // (if it e.g clashes with another name)
                        string newName = manager.GetName(deserialized);
                        if (newName != objectEntry.Name)
                        {
                            objectEntry.Name = newName;
                        }
                    }
                }
                else
                {
                    foreach (MemberEntry memberEntry in objectEntry.Members.Values)
                    {
                        CodeDomSerializer serializer = (CodeDomSerializer)manager.GetSerializer(objectEntry.Type,
                                                                                                typeof(CodeDomSerializer));
                        if (serializer != null)
                        {
                            serializer.Deserialize(manager, memberEntry.Serialized);
                        }
                    }
                }

                return(deserialized);
            }
Ejemplo n.º 2
0
        protected override void PerformLoad(IDesignerSerializationManager manager)
        {
            if (manager == null)
            {
                throw new ArgumentNullException("manager");
            }

            CodeCompileUnit document = this.Parse();

            if (document == null)
            {
                throw new NotSupportedException("The language did not provide a code parser for this file");
            }

            string namespaceName             = null;
            CodeTypeDeclaration rootDocument = GetFirstCodeTypeDecl(document, out namespaceName);

            if (rootDocument == null)
            {
                throw new InvalidOperationException("Cannot find a declaration in a namespace to load.");
            }

            _rootSerializer = manager.GetSerializer(manager.GetType(rootDocument.BaseTypes[0].BaseType),
                                                    typeof(RootCodeDomSerializer)) as CodeDomSerializer;
            if (_rootSerializer == null)
            {
                throw new InvalidOperationException("Serialization not supported for this class");
            }

            _rootSerializer.Deserialize(manager, rootDocument);

            base.SetBaseComponentClassName(namespaceName + "." + rootDocument.Name);
        }
Ejemplo n.º 3
0
 public CodeDomSerializationProvider()
 {
     _componentSerializer  = new ComponentCodeDomSerializer();
     _propertySerializer   = new PropertyCodeDomSerializer();
     _eventSerializer      = new EventCodeDomSerializer();
     _collectionSerializer = new CollectionCodeDomSerializer();
     _primitiveSerializer  = new PrimitiveCodeDomSerializer();
     _rootSerializer       = new RootCodeDomSerializer();
     _enumSerializer       = new EnumCodeDomSerializer();
     _othersSerializer     = new CodeDomSerializer();
 }
        protected CodeDomSerializer GetSerializer(IDesignerSerializationManager manager, object instance)
        {
            DesignerSerializerAttribute attrInstance, attrType;

            attrType = attrInstance = null;

            CodeDomSerializer serializer = null;

            if (instance == null)
            {
                serializer = this.GetSerializer(manager, null);
            }
            else
            {
                AttributeCollection attributes = TypeDescriptor.GetAttributes(instance);
                foreach (Attribute a in attributes)
                {
                    DesignerSerializerAttribute designerAttr = a as DesignerSerializerAttribute;
                    if (designerAttr != null && manager.GetType(designerAttr.SerializerBaseTypeName) == typeof(CodeDomSerializer))
                    {
                        attrInstance = designerAttr;
                        break;
                    }
                }

                attributes = TypeDescriptor.GetAttributes(instance.GetType());
                foreach (Attribute a in attributes)
                {
                    DesignerSerializerAttribute designerAttr = a as DesignerSerializerAttribute;
                    if (designerAttr != null && manager.GetType(designerAttr.SerializerBaseTypeName) == typeof(CodeDomSerializer))
                    {
                        attrType = designerAttr;
                        break;
                    }
                }

                // if there is metadata modification in the instance then create the specified serializer instead of the one
                // in the Type.
                if (attrType != null && attrInstance != null && attrType.SerializerTypeName != attrInstance.SerializerTypeName)
                {
                    serializer = Activator.CreateInstance(manager.GetType(attrInstance.SerializerTypeName)) as CodeDomSerializer;
                }
                else
                {
                    serializer = this.GetSerializer(manager, instance.GetType());
                }
            }

            return(serializer);
        }
Ejemplo n.º 5
0
        private void SerializeContentProperty(IDesignerSerializationManager manager, object component,
                                              PropertyDescriptor descriptor, CodeStatementCollection statements)
        {
            CodePropertyReferenceExpression propRef = new CodePropertyReferenceExpression();

            propRef.PropertyName = descriptor.Name;
            object value = descriptor.GetValue(component);

            ExpressionContext expressionCtx = manager.Context[typeof(ExpressionContext)] as ExpressionContext;

            if (expressionCtx != null && expressionCtx.PresetValue == component)
            {
                propRef.TargetObject = expressionCtx.Expression;
            }
            else
            {
                manager.Context.Push(new CodeStatementCollection());
                propRef.TargetObject = TryGetCachedExpression(manager, component, propRef, value);
                manager.Context.Pop();
            }

            CodeDomSerializer serializer = manager.GetSerializer(value.GetType(), typeof(CodeDomSerializer)) as CodeDomSerializer;

            if (propRef.TargetObject != null && serializer != null)
            {
                // request full serialization (presetvalue == instance)
                //
                manager.Context.Push(new ExpressionContext(propRef, propRef.GetType(), component, value));
                object serialized = serializer.Serialize(manager, value);
                manager.Context.Pop();

                CodeStatementCollection serializedStatements = serialized as CodeStatementCollection;
                if (serializedStatements != null)
                {
                    statements.AddRange(serializedStatements);
                }

                CodeStatement serializedStatement = serialized as CodeStatement;
                if (serializedStatement != null)
                {
                    statements.Add(serializedStatement);
                }

                CodeExpression serializedExpr = serialized as CodeExpression;
                if (serializedExpr != null)
                {
                    statements.Add(new CodeAssignStatement(propRef, serializedExpr));
                }
            }
        }
        private void SerializeComponent(IDesignerSerializationManager manager, IComponent component)
        {
            CodeDomSerializer serializer = base.GetSerializer(manager, component) as CodeDomSerializer;              // ComponentCodeDomSerializer

            if (serializer != null)
            {
                this.Code.AddField(new CodeMemberField(component.GetType(), manager.GetName(component)));

                CodeStatementCollection statements = (CodeStatementCollection)serializer.Serialize(manager, component);

                CodeStatement ctorStatement = ExtractCtorStatement(manager, statements, component);
                if (ctorStatement != null)
                {
                    Code.AddPreInitStatement(ctorStatement);
                }
                Code.AddInitStatements(statements);
            }
        }
Ejemplo n.º 7
0
        private void SerializeComponent(IDesignerSerializationManager manager, IComponent component)
        {
            CodeDomSerializer serializer = base.GetSerializer(manager, component) as CodeDomSerializer;              // ComponentCodeDomSerializer

            if (serializer != null)
            {
                this._codeMap.AddField(new CodeMemberField(component.GetType(), manager.GetName(component)));
                // statements can be a CodeExpression if the full serialization has been completed prior
                // to this serialization call (e.g when it is requested during the serialization of another
                // component.
                //
                CodeStatementCollection statements = serializer.Serialize(manager, component) as CodeStatementCollection;
                if (statements != null)
                {
                    _codeMap.Add(statements);
                }
                CodeStatement statement = serializer.Serialize(manager, component) as CodeStatement;
                if (statement != null)
                {
                    _codeMap.Add(statement);
                }
            }
        }
Ejemplo n.º 8
0
            private void Serialize(IServiceProvider provider)
            {
                if (provider == null || _objects == null)
                {
                    return;
                }

                // Use a new serialization manager to prevent from "deadlocking" the surface one
                // by trying to create new session when one currently exists
                //
                InstanceRedirectorDesignerSerializationManager manager =
                    new InstanceRedirectorDesignerSerializationManager(provider, null, false);

                ((IDesignerSerializationManager)manager).AddSerializationProvider(CodeDomSerializationProvider.Instance);
                IDisposable session = manager.CreateSession();

                foreach (ObjectEntry objectEntry in _objects.Values)
                {
                    if (objectEntry.IsEntireObject)
                    {
                        CodeDomSerializer serializer = (CodeDomSerializer)((IDesignerSerializationManager)manager).GetSerializer(objectEntry.Type,
                                                                                                                                 typeof(CodeDomSerializer));
                        if (serializer != null)
                        {
                            object serialized = null;
                            if (objectEntry.Absolute)
                            {
                                serialized = serializer.SerializeAbsolute(manager,
                                                                          objectEntry.Instance);
                            }
                            else
                            {
                                serialized = serializer.Serialize(manager, objectEntry.Instance);
                            }
                            objectEntry.Serialized = serialized;
                        }
                    }
                    else
                    {
                        foreach (MemberEntry memberEntry in objectEntry.Members.Values)
                        {
                            CodeDomSerializer serializer = (CodeDomSerializer)((IDesignerSerializationManager)manager).GetSerializer(
                                objectEntry.Type, typeof(CodeDomSerializer));
                            if (serializer != null)
                            {
                                object serialized = null;
                                if (memberEntry.Absolute)
                                {
                                    serialized = serializer.SerializeMemberAbsolute(manager,
                                                                                    objectEntry.Instance,
                                                                                    memberEntry.Descriptor);
                                }
                                else
                                {
                                    serialized = serializer.SerializeMember(manager,
                                                                            objectEntry.Instance,
                                                                            memberEntry.Descriptor);
                                }
                                memberEntry.Serialized = serialized;
                            }
                        }
                    }
                }
                _errors = manager.Errors;
                ReportErrors(provider, _errors);
                session.Dispose();
            }
        protected CodeExpression SerializeToExpression(IDesignerSerializationManager manager, object instance)
        {
            if (manager == null)
            {
                throw new ArgumentNullException("manager");
            }

            CodeExpression expression = null;

            if (instance != null)
            {
                expression = this.GetExpression(manager, instance);                  // 1 - IDesignerSerializationManager.GetExpression
            }
            if (expression == null)
            {
                CodeDomSerializer serializer = this.GetSerializer(manager, instance);                  // 2 - manager.GetSerializer().Serialize()
                if (serializer != null)
                {
                    object serialized = serializer.Serialize(manager, instance);
                    expression = serialized as CodeExpression;                     // 3 - CodeStatement or CodeStatementCollection
                    if (expression == null)
                    {
                        CodeStatement           statement  = serialized as CodeStatement;
                        CodeStatementCollection statements = serialized as CodeStatementCollection;

                        if (statement != null || statements != null)
                        {
                            CodeStatementCollection contextStatements = null;

                            StatementContext context = manager.Context[typeof(StatementContext)] as StatementContext;
                            if (context != null && instance != null)
                            {
                                contextStatements = context.StatementCollection[instance];
                            }

                            if (contextStatements == null)
                            {
                                contextStatements = manager.Context[typeof(CodeStatementCollection)] as CodeStatementCollection;
                            }

                            if (contextStatements != null)
                            {
                                if (statements != null)
                                {
                                    contextStatements.AddRange(statements);
                                }
                                else
                                {
                                    contextStatements.Add(statement);
                                }
                            }
                        }
                    }
                    if (expression == null && instance != null)
                    {
                        expression = this.GetExpression(manager, instance);                          // 4
                    }
                    if (expression == null)
                    {
                        Console.WriteLine("SerializeToExpression: " + instance + " failed.");
                    }
                }
            }
            return(expression);
        }
Ejemplo n.º 10
0
        public override object Serialize(IDesignerSerializationManager manager, object value)
        {
            if (manager == null)
            {
                throw new ArgumentNullException("manager");
            }
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            // Add the provider to supply the ComponentCodeSerializer, Primitives..., etc
            manager.AddSerializationProvider(_provider);
            RootContext rootContext = new RootContext(new CodeThisReferenceExpression(), value);

            manager.Context.Push(rootContext);

            // Initialize code map
            if (_codeMap != null)
            {
                _codeMap = new CodeMap(value.GetType(), manager.GetName(value));
            }
            _codeMap.Clear();
            CodeStatementCollection statements = null;
            CodeDomSerializer       serializer = null;

            foreach (object component in ((IComponent)value).Site.Container.Components)
            {
                if (!Object.ReferenceEquals(component, value))
                {
                    serializer = base.GetSerializer(manager, component) as CodeDomSerializer;                      // ComponentCodeDomSerializer
                    if (serializer != null)
                    {
                        // add an expressioncontext to inform the component that we want it fully serialized (it is in context)
                        ExpressionContext context = new ExpressionContext(null, null, null, component);
                        manager.Context.Push(context);

                        _codeMap.AddField(new CodeMemberField(value.GetType(), manager.GetName(value)));
                        statements = (CodeStatementCollection)serializer.Serialize(manager, component);

                        manager.Context.Pop();
                        // XXX: what if there are more than one objects constructed by the serializer?
                        // this will only add the first one on the statements list.
                        CodeStatement ctorStatement = ExtractCtorStatement(statements);
                        if (ctorStatement != null)
                        {
                            _codeMap.AddPreInitStatement(ctorStatement);
                        }
                        _codeMap.AddInitStatements(statements);
                    }
                }
            }

            // Serializer root component
            //
            statements = new CodeStatementCollection();
            base.SerializeProperties(manager, statements, value, new Attribute[0]);
            base.SerializeEvents(manager, statements, value, new Attribute[0]);
            _codeMap.AddInitStatements(statements);

            manager.Context.Pop();
            return(_codeMap.GenerateClass());
        }