public override object Serialize(IDesignerSerializationManager manager, object value)
        {
            if (manager == null)
            {
                throw new ArgumentNullException("manager");
            }
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if (_codeMap == null)
            {
                _codeMap = new CodeMap(value.GetType(), manager.GetName(value));
            }
            _codeMap.Clear();

            RootContext rootContext = new RootContext(new CodeThisReferenceExpression(), value);

            manager.Context.Push(rootContext);

            this.SerializeComponents(manager, ((IComponent)value).Site.Container.Components, (IComponent)value);

            // Serialize root component
            //
            CodeStatementCollection statements = new CodeStatementCollection();

            statements.Add(new CodeCommentStatement(String.Empty));
            statements.Add(new CodeCommentStatement(manager.GetName(value)));
            statements.Add(new CodeCommentStatement(String.Empty));
            // Note that during the serialization process below ComponentCodeDomSerializer
            // will be invoked to serialize the rootcomponent during expression serialization.
            // It will check for RootContext and return that.
            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());
        }
Esempio n. 2
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());
        }