Esempio n. 1
0
        public static WorkflowTheme Load(IDesignerSerializationManager serializationManager, string themeFilePath)
        {
            if (serializationManager == null)
            {
                throw new ArgumentNullException("serializationManager");
            }
            WorkflowTheme theme = null;

            if ((themeFilePath != null) && File.Exists(themeFilePath))
            {
                XmlReader reader = XmlReader.Create(themeFilePath);
                ThemeSerializationProvider provider = new ThemeSerializationProvider();
                try
                {
                    serializationManager.AddSerializationProvider(provider);
                    WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();
                    theme = serializer.Deserialize(serializationManager, reader) as WorkflowTheme;
                }
                finally
                {
                    serializationManager.RemoveSerializationProvider(provider);
                    reader.Close();
                }
                if (theme != null)
                {
                    theme.filePath = themeFilePath;
                }
            }
            return(theme);
        }
        protected override void Initialize()
        {
            CodeDomLocalizationModel model = FormsDesigner.Gui.OptionPanels.LocalizationModelOptionsPanel.DefaultLocalizationModel;

            if (FormsDesigner.Gui.OptionPanels.LocalizationModelOptionsPanel.KeepLocalizationModel)
            {
                // Try to find out the current localization model of the designed form
                CodeDomLocalizationModel existingModel = this.GetCurrentLocalizationModelFromDesignedFile();
                if (existingModel != CodeDomLocalizationModel.None)
                {
                    LoggingService.Debug("Determined existing localization model, using that: " + existingModel.ToString());
                    model = existingModel;
                }
                else
                {
                    LoggingService.Debug("Could not determine existing localization model, using default: " + model.ToString());
                }
            }
            else
            {
                LoggingService.Debug("Using default localization model: " + model.ToString());
            }

            CodeDomLocalizationProvider   localizationProvider = new CodeDomLocalizationProvider(designerLoaderHost, model);
            IDesignerSerializationManager manager = (IDesignerSerializationManager)designerLoaderHost.GetService(typeof(IDesignerSerializationManager));

            manager.AddSerializationProvider(new SharpDevelopSerializationProvider());
            manager.AddSerializationProvider(localizationProvider);
            base.Initialize();

            IComponentChangeService componentChangeService = (IComponentChangeService)this.GetService(typeof(IComponentChangeService));

            if (componentChangeService != null)
            {
                LoggingService.Debug("Forms designer: Adding ComponentAdded handler for nested container setup");
                componentChangeService.ComponentAdded += ComponentContainerSetUp;
            }
            else
            {
                LoggingService.Warn("Forms designer: Cannot add ComponentAdded handler for nested container setup because IComponentChangeService is unavailable");
            }
        }
Esempio n. 3
0
        protected override void Initialize()
        {
            base.Initialize();
            base.LoaderHost.AddService(typeof(IDesignerSerializationService), this);
            base.LoaderHost.AddService(typeof(INameCreationService), this);
            base.LoaderHost.AddService(typeof(ComponentSerializationService), new
                                       CodeDomComponentSerializationService(base.LoaderHost));
            IDesignerSerializationManager manager = base.LoaderHost.GetService(typeof(IDesignerSerializationManager)) as IDesignerSerializationManager;

            if (manager != null)
            {
                manager.AddSerializationProvider(new CodeDomSerializationProvider());
            }
        }
		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 ();
		}
        public override object Serialize(IDesignerSerializationManager manager, object value)
        {
            if ((manager == null) || (value == null))
            {
                throw new ArgumentNullException((manager == null) ? "manager" : "value");
            }
            CodeTypeDeclaration declaration = new CodeTypeDeclaration(manager.GetName(value));
            RootContext         context     = new RootContext(new CodeThisReferenceExpression(), value);

            using (CodeDomSerializerBase.TraceScope("RootCodeDomSerializer::Serialize"))
            {
                declaration.BaseTypes.Add(value.GetType());
                this.containerRequired = false;
                manager.Context.Push(context);
                manager.Context.Push(this);
                manager.Context.Push(declaration);
                if (!(manager is DesignerSerializationManager))
                {
                    manager.AddSerializationProvider(new CodeDomSerializationProvider());
                }
                try
                {
                    if (value is IComponent)
                    {
                        ISite site = ((IComponent)value).Site;
                        if (site == null)
                        {
                            return(declaration);
                        }
                        ICollection      components = site.Container.Components;
                        StatementContext context2   = new StatementContext();
                        context2.StatementCollection.Populate(components);
                        manager.Context.Push(context2);
                        try
                        {
                            foreach (IComponent component in components)
                            {
                                if ((component != value) && !base.IsSerialized(manager, component))
                                {
                                    if (base.GetSerializer(manager, component) != null)
                                    {
                                        base.SerializeToExpression(manager, component);
                                    }
                                    else
                                    {
                                        manager.ReportError(System.Design.SR.GetString("SerializerNoSerializerForComponent", new object[] { component.GetType().FullName }));
                                    }
                                }
                            }
                            manager.Context.Push(value);
                            try
                            {
                                if ((base.GetSerializer(manager, value) != null) && !base.IsSerialized(manager, value))
                                {
                                    base.SerializeToExpression(manager, value);
                                }
                                else
                                {
                                    manager.ReportError(System.Design.SR.GetString("SerializerNoSerializerForComponent", new object[] { value.GetType().FullName }));
                                }
                            }
                            finally
                            {
                                manager.Context.Pop();
                            }
                        }
                        finally
                        {
                            manager.Context.Pop();
                        }
                        CodeMemberMethod method = new CodeMemberMethod {
                            Name       = this.InitMethodName,
                            Attributes = MemberAttributes.Private
                        };
                        declaration.Members.Add(method);
                        ArrayList elements = new ArrayList();
                        foreach (object obj2 in components)
                        {
                            if (obj2 != value)
                            {
                                elements.Add(context2.StatementCollection[obj2]);
                            }
                        }
                        if (context2.StatementCollection[value] != null)
                        {
                            elements.Add(context2.StatementCollection[value]);
                        }
                        if (this.ContainerRequired)
                        {
                            this.SerializeContainerDeclaration(manager, method.Statements);
                        }
                        this.SerializeElementsToStatements(elements, method.Statements);
                    }
                    return(declaration);
                }
                finally
                {
                    manager.Context.Pop();
                    manager.Context.Pop();
                    manager.Context.Pop();
                }
            }
            return(declaration);
        }
        public override object Deserialize(IDesignerSerializationManager manager, object codeObject)
        {
            if ((manager == null) || (codeObject == null))
            {
                throw new ArgumentNullException((manager == null) ? "manager" : "codeObject");
            }
            object obj2 = null;

            using (CodeDomSerializerBase.TraceScope("RootCodeDomSerializer::Deserialize"))
            {
                if (!(codeObject is CodeTypeDeclaration))
                {
                    throw new ArgumentException(System.Design.SR.GetString("SerializerBadElementType", new object[] { typeof(CodeTypeDeclaration).FullName }));
                }
                bool            caseInsensitive = false;
                CodeDomProvider service         = manager.GetService(typeof(CodeDomProvider)) as CodeDomProvider;
                if (service != null)
                {
                    caseInsensitive = (service.LanguageOptions & LanguageOptions.CaseInsensitive) != LanguageOptions.None;
                }
                CodeTypeDeclaration declaration = (CodeTypeDeclaration)codeObject;
                CodeTypeReference   reference   = null;
                Type type = null;
                foreach (CodeTypeReference reference2 in declaration.BaseTypes)
                {
                    Type type2 = manager.GetType(CodeDomSerializerBase.GetTypeNameFromCodeTypeReference(manager, reference2));
                    if ((type2 != null) && !type2.IsInterface)
                    {
                        reference = reference2;
                        type      = type2;
                        break;
                    }
                }
                if (type == null)
                {
                    Exception exception = new SerializationException(System.Design.SR.GetString("SerializerTypeNotFound", new object[] { reference.BaseType }))
                    {
                        HelpLink = "SerializerTypeNotFound"
                    };
                    throw exception;
                }
                if (type.IsAbstract)
                {
                    Exception exception2 = new SerializationException(System.Design.SR.GetString("SerializerTypeAbstract", new object[] { type.FullName }))
                    {
                        HelpLink = "SerializerTypeAbstract"
                    };
                    throw exception2;
                }
                ResolveNameEventHandler handler = new ResolveNameEventHandler(this.OnResolveName);
                manager.ResolveName += handler;
                if (!(manager is DesignerSerializationManager))
                {
                    manager.AddSerializationProvider(new CodeDomSerializationProvider());
                }
                obj2                = manager.CreateInstance(type, null, declaration.Name, true);
                this.nameTable      = new HybridDictionary(declaration.Members.Count, caseInsensitive);
                this.statementTable = new HybridDictionary(declaration.Members.Count, caseInsensitive);
                this.initMethod     = null;
                RootContext context = new RootContext(new CodeThisReferenceExpression(), obj2);
                manager.Context.Push(context);
                try
                {
                    foreach (CodeTypeMember member in declaration.Members)
                    {
                        if (member is CodeMemberField)
                        {
                            if (string.Compare(member.Name, declaration.Name, caseInsensitive, CultureInfo.InvariantCulture) != 0)
                            {
                                this.nameTable[member.Name] = member;
                            }
                        }
                        else if ((this.initMethod == null) && (member is CodeMemberMethod))
                        {
                            CodeMemberMethod method = (CodeMemberMethod)member;
                            if ((string.Compare(method.Name, this.InitMethodName, caseInsensitive, CultureInfo.InvariantCulture) == 0) && (method.Parameters.Count == 0))
                            {
                                this.initMethod = method;
                            }
                        }
                    }
                    if (this.initMethod != null)
                    {
                        foreach (CodeStatement statement in this.initMethod.Statements)
                        {
                            CodeVariableDeclarationStatement statement2 = statement as CodeVariableDeclarationStatement;
                            if (statement2 != null)
                            {
                                this.nameTable[statement2.Name] = statement;
                            }
                        }
                    }
                    if (this.nameTable[declaration.Name] != null)
                    {
                        this.nameTable[declaration.Name] = obj2;
                    }
                    if (this.initMethod != null)
                    {
                        this.FillStatementTable(this.initMethod, declaration.Name);
                    }
                    PropertyDescriptor descriptor = manager.Properties["SupportsStatementGeneration"];
                    if (((descriptor != null) && (descriptor.PropertyType == typeof(bool))) && ((bool)descriptor.GetValue(manager)))
                    {
                        foreach (string str in this.nameTable.Keys)
                        {
                            CodeDomSerializerBase.OrderedCodeStatementCollection statements = (CodeDomSerializerBase.OrderedCodeStatementCollection) this.statementTable[str];
                            if (statements != null)
                            {
                                bool flag2 = false;
                                foreach (CodeStatement statement3 in statements)
                                {
                                    object obj3 = statement3.UserData["GeneratedStatement"];
                                    if (((obj3 == null) || !(obj3 is bool)) || !((bool)obj3))
                                    {
                                        flag2 = true;
                                        break;
                                    }
                                }
                                if (!flag2)
                                {
                                    this.statementTable.Remove(str);
                                }
                            }
                        }
                    }
                    IContainer container = (IContainer)manager.GetService(typeof(IContainer));
                    if (container != null)
                    {
                        foreach (object obj4 in container.Components)
                        {
                            base.DeserializePropertiesFromResources(manager, obj4, designTimeProperties);
                        }
                    }
                    object[] array = new object[this.statementTable.Values.Count];
                    this.statementTable.Values.CopyTo(array, 0);
                    Array.Sort(array, StatementOrderComparer.Default);
                    foreach (CodeDomSerializerBase.OrderedCodeStatementCollection statements2 in array)
                    {
                        string name = statements2.Name;
                        if ((name != null) && !name.Equals(declaration.Name))
                        {
                            this.DeserializeName(manager, name);
                        }
                    }
                    CodeStatementCollection statements3 = (CodeStatementCollection)this.statementTable[declaration.Name];
                    if ((statements3 != null) && (statements3.Count > 0))
                    {
                        foreach (CodeStatement statement4 in statements3)
                        {
                            base.DeserializeStatement(manager, statement4);
                        }
                    }
                    return(obj2);
                }
                finally
                {
                    manager.ResolveName -= handler;
                    this.initMethod      = null;
                    this.nameTable       = null;
                    this.statementTable  = null;
                    manager.Context.Pop();
                }
            }
            return(obj2);
        }
 public override object Deserialize(IDesignerSerializationManager manager, object codeObject)
 {
     if ((manager == null) || (codeObject == null))
     {
         throw new ArgumentNullException((manager == null) ? "manager" : "codeObject");
     }
     object obj2 = null;
     using (CodeDomSerializerBase.TraceScope("RootCodeDomSerializer::Deserialize"))
     {
         if (!(codeObject is CodeTypeDeclaration))
         {
             throw new ArgumentException(System.Design.SR.GetString("SerializerBadElementType", new object[] { typeof(CodeTypeDeclaration).FullName }));
         }
         bool caseInsensitive = false;
         CodeDomProvider service = manager.GetService(typeof(CodeDomProvider)) as CodeDomProvider;
         if (service != null)
         {
             caseInsensitive = (service.LanguageOptions & LanguageOptions.CaseInsensitive) != LanguageOptions.None;
         }
         CodeTypeDeclaration declaration = (CodeTypeDeclaration) codeObject;
         CodeTypeReference reference = null;
         Type type = null;
         foreach (CodeTypeReference reference2 in declaration.BaseTypes)
         {
             Type type2 = manager.GetType(CodeDomSerializerBase.GetTypeNameFromCodeTypeReference(manager, reference2));
             if ((type2 != null) && !type2.IsInterface)
             {
                 reference = reference2;
                 type = type2;
                 break;
             }
         }
         if (type == null)
         {
             Exception exception = new SerializationException(System.Design.SR.GetString("SerializerTypeNotFound", new object[] { reference.BaseType })) {
                 HelpLink = "SerializerTypeNotFound"
             };
             throw exception;
         }
         if (type.IsAbstract)
         {
             Exception exception2 = new SerializationException(System.Design.SR.GetString("SerializerTypeAbstract", new object[] { type.FullName })) {
                 HelpLink = "SerializerTypeAbstract"
             };
             throw exception2;
         }
         ResolveNameEventHandler handler = new ResolveNameEventHandler(this.OnResolveName);
         manager.ResolveName += handler;
         if (!(manager is DesignerSerializationManager))
         {
             manager.AddSerializationProvider(new CodeDomSerializationProvider());
         }
         obj2 = manager.CreateInstance(type, null, declaration.Name, true);
         this.nameTable = new HybridDictionary(declaration.Members.Count, caseInsensitive);
         this.statementTable = new HybridDictionary(declaration.Members.Count, caseInsensitive);
         this.initMethod = null;
         RootContext context = new RootContext(new CodeThisReferenceExpression(), obj2);
         manager.Context.Push(context);
         try
         {
             foreach (CodeTypeMember member in declaration.Members)
             {
                 if (member is CodeMemberField)
                 {
                     if (string.Compare(member.Name, declaration.Name, caseInsensitive, CultureInfo.InvariantCulture) != 0)
                     {
                         this.nameTable[member.Name] = member;
                     }
                 }
                 else if ((this.initMethod == null) && (member is CodeMemberMethod))
                 {
                     CodeMemberMethod method = (CodeMemberMethod) member;
                     if ((string.Compare(method.Name, this.InitMethodName, caseInsensitive, CultureInfo.InvariantCulture) == 0) && (method.Parameters.Count == 0))
                     {
                         this.initMethod = method;
                     }
                 }
             }
             if (this.initMethod != null)
             {
                 foreach (CodeStatement statement in this.initMethod.Statements)
                 {
                     CodeVariableDeclarationStatement statement2 = statement as CodeVariableDeclarationStatement;
                     if (statement2 != null)
                     {
                         this.nameTable[statement2.Name] = statement;
                     }
                 }
             }
             if (this.nameTable[declaration.Name] != null)
             {
                 this.nameTable[declaration.Name] = obj2;
             }
             if (this.initMethod != null)
             {
                 this.FillStatementTable(this.initMethod, declaration.Name);
             }
             PropertyDescriptor descriptor = manager.Properties["SupportsStatementGeneration"];
             if (((descriptor != null) && (descriptor.PropertyType == typeof(bool))) && ((bool) descriptor.GetValue(manager)))
             {
                 foreach (string str in this.nameTable.Keys)
                 {
                     CodeDomSerializerBase.OrderedCodeStatementCollection statements = (CodeDomSerializerBase.OrderedCodeStatementCollection) this.statementTable[str];
                     if (statements != null)
                     {
                         bool flag2 = false;
                         foreach (CodeStatement statement3 in statements)
                         {
                             object obj3 = statement3.UserData["GeneratedStatement"];
                             if (((obj3 == null) || !(obj3 is bool)) || !((bool) obj3))
                             {
                                 flag2 = true;
                                 break;
                             }
                         }
                         if (!flag2)
                         {
                             this.statementTable.Remove(str);
                         }
                     }
                 }
             }
             IContainer container = (IContainer) manager.GetService(typeof(IContainer));
             if (container != null)
             {
                 foreach (object obj4 in container.Components)
                 {
                     base.DeserializePropertiesFromResources(manager, obj4, designTimeProperties);
                 }
             }
             object[] array = new object[this.statementTable.Values.Count];
             this.statementTable.Values.CopyTo(array, 0);
             Array.Sort(array, StatementOrderComparer.Default);
             foreach (CodeDomSerializerBase.OrderedCodeStatementCollection statements2 in array)
             {
                 string name = statements2.Name;
                 if ((name != null) && !name.Equals(declaration.Name))
                 {
                     this.DeserializeName(manager, name);
                 }
             }
             CodeStatementCollection statements3 = (CodeStatementCollection) this.statementTable[declaration.Name];
             if ((statements3 != null) && (statements3.Count > 0))
             {
                 foreach (CodeStatement statement4 in statements3)
                 {
                     base.DeserializeStatement(manager, statement4);
                 }
             }
             return obj2;
         }
         finally
         {
             manager.ResolveName -= handler;
             this.initMethod = null;
             this.nameTable = null;
             this.statementTable = null;
             manager.Context.Pop();
         }
     }
     return obj2;
 }
 public override object Serialize(IDesignerSerializationManager manager, object value)
 {
     if ((manager == null) || (value == null))
     {
         throw new ArgumentNullException((manager == null) ? "manager" : "value");
     }
     CodeTypeDeclaration declaration = new CodeTypeDeclaration(manager.GetName(value));
     RootContext context = new RootContext(new CodeThisReferenceExpression(), value);
     using (CodeDomSerializerBase.TraceScope("RootCodeDomSerializer::Serialize"))
     {
         declaration.BaseTypes.Add(value.GetType());
         this.containerRequired = false;
         manager.Context.Push(context);
         manager.Context.Push(this);
         manager.Context.Push(declaration);
         if (!(manager is DesignerSerializationManager))
         {
             manager.AddSerializationProvider(new CodeDomSerializationProvider());
         }
         try
         {
             if (value is IComponent)
             {
                 ISite site = ((IComponent) value).Site;
                 if (site == null)
                 {
                     return declaration;
                 }
                 ICollection components = site.Container.Components;
                 StatementContext context2 = new StatementContext();
                 context2.StatementCollection.Populate(components);
                 manager.Context.Push(context2);
                 try
                 {
                     foreach (IComponent component in components)
                     {
                         if ((component != value) && !base.IsSerialized(manager, component))
                         {
                             if (base.GetSerializer(manager, component) != null)
                             {
                                 base.SerializeToExpression(manager, component);
                             }
                             else
                             {
                                 manager.ReportError(System.Design.SR.GetString("SerializerNoSerializerForComponent", new object[] { component.GetType().FullName }));
                             }
                         }
                     }
                     manager.Context.Push(value);
                     try
                     {
                         if ((base.GetSerializer(manager, value) != null) && !base.IsSerialized(manager, value))
                         {
                             base.SerializeToExpression(manager, value);
                         }
                         else
                         {
                             manager.ReportError(System.Design.SR.GetString("SerializerNoSerializerForComponent", new object[] { value.GetType().FullName }));
                         }
                     }
                     finally
                     {
                         manager.Context.Pop();
                     }
                 }
                 finally
                 {
                     manager.Context.Pop();
                 }
                 CodeMemberMethod method = new CodeMemberMethod {
                     Name = this.InitMethodName,
                     Attributes = MemberAttributes.Private
                 };
                 declaration.Members.Add(method);
                 ArrayList elements = new ArrayList();
                 foreach (object obj2 in components)
                 {
                     if (obj2 != value)
                     {
                         elements.Add(context2.StatementCollection[obj2]);
                     }
                 }
                 if (context2.StatementCollection[value] != null)
                 {
                     elements.Add(context2.StatementCollection[value]);
                 }
                 if (this.ContainerRequired)
                 {
                     this.SerializeContainerDeclaration(manager, method.Statements);
                 }
                 this.SerializeElementsToStatements(elements, method.Statements);
             }
             return declaration;
         }
         finally
         {
             manager.Context.Pop();
             manager.Context.Pop();
             manager.Context.Pop();
         }
     }
     return declaration;
 }
Esempio n. 9
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());
        }
Esempio n. 10
0
        public override object Serialize(IDesignerSerializationManager manager, object value)
        {
            CodeDomSerializer serial = GetBaseComponentSerializer(manager);

            if (serial == null)
            {
                return(null);
            }

            CodeStatementCollection statements = (CodeStatementCollection)serial.Serialize(manager, value);

            //serializer for web controls.
            if (!(manager.GetSerializer(typeof(System.Web.UI.Control), typeof(CodeDomSerializer)) is WebControlSerializer))
            {
                manager.AddSerializationProvider(new WebControlSerializationProvider());
            }

            IDesignerHost host = (IDesignerHost)manager.GetService(typeof(IDesignerHost));

            if (host.RootComponent == value)
            {
                return(statements);
            }

            statements.AddRange(GetCommentHeader("Mapper code"));
            DataUIMapper   cn    = (DataUIMapper)value;
            CodeExpression cnref = SerializeToExpression(manager, value);

            #region Mapping property serialization

            CodePropertyReferenceExpression propref = new CodePropertyReferenceExpression(cnref, "Mappings");
            foreach (MapperInfo mi in cn.Mappings)
            {
                MapperInfo info = mi;
                if (info.ControlID != String.Empty && info.ControlProperty != null &&
                    info.DataProperty != String.Empty)
                {
                    object ctl = manager.GetInstance(info.ControlID);
                    //if (ctl == null)
                    //{
                    //    manager.ReportError(String.Format("Control '{0}' associated with the view mapping in controller '{1}' doesn't exist in the page.", info.ControlID, manager.GetName(value)));
                    //    continue;
                    //}
                    if (ctl.GetType().GetProperty(info.ControlProperty) == null)
                    {
                        manager.ReportError(String.Format("Control property '{0}' in control '{1}' associated with the view mapping in datauimapper '{2}' doesn't exist.", info.ControlProperty, info.ControlID, manager.GetName(value)));
                        continue;
                    }

                    statements.Add(
                        new CodeMethodInvokeExpression(
                            propref, "Add",
                            new CodeExpression[]
                    {
                        new CodeObjectCreateExpression(
                            typeof(MapperInfo),
                            new CodeExpression[] {
                            new CodePrimitiveExpression(info.ControlID),
                            new CodePrimitiveExpression(info.ControlProperty),
                            new CodePrimitiveExpression(info.DataProperty), new CodePrimitiveExpression((int)info.Format)
                        }
                            )
                    }
                            ));
                }
            }

            #endregion

            statements.Add(
                new CodeCommentStatement("Connect the host environment."));

            if (host.RootComponent as System.Windows.Forms.Form != null)
            {
                CodeObjectCreateExpression adapter = new CodeObjectCreateExpression(typeof(Adapter.WindowsFormsAdapter), new CodeExpression[0]);
                CodeExpression             connect = new CodeMethodInvokeExpression(adapter, "Connect",
                                                                                    new CodeExpression[] {
                    cnref,
                    new CodeThisReferenceExpression(),
                });
                statements.Add(connect);
            }
            else if (host.RootComponent as System.Web.UI.Page != null)
            {
                CodeObjectCreateExpression adapter = new CodeObjectCreateExpression(typeof(Adapter.WebFormsAdapter), new CodeExpression[0]);
                CodeExpression             connect = new CodeMethodInvokeExpression(adapter, "Connect",
                                                                                    new CodeExpression[] {
                    cnref,
                    new CodeThisReferenceExpression(),
                });
                statements.Add(connect);
            }

            return(statements);
        }