Ejemplo n.º 1
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);
        }
            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);
            }
        private object DeserializeName(IDesignerSerializationManager manager, string name, CodeStatementCollection statements)
        {
            object obj2 = null;

            using (CodeDomSerializerBase.TraceScope("RootCodeDomSerializer::DeserializeName"))
            {
                obj2 = this._nameTable[name];
                CodeObject      obj3     = obj2 as CodeObject;
                string          typeName = null;
                CodeMemberField field    = null;
                if (obj3 != null)
                {
                    obj2 = null;
                    this._nameTable[name] = null;
                    CodeVariableDeclarationStatement statement = obj3 as CodeVariableDeclarationStatement;
                    if (statement != null)
                    {
                        typeName = CodeDomSerializerBase.GetTypeNameFromCodeTypeReference(manager, statement.Type);
                    }
                    else
                    {
                        field = obj3 as CodeMemberField;
                        if (field != null)
                        {
                            typeName = CodeDomSerializerBase.GetTypeNameFromCodeTypeReference(manager, field.Type);
                        }
                        else
                        {
                            CodeExpression expression = obj3 as CodeExpression;
                            RootContext    context    = manager.Context[typeof(RootContext)] as RootContext;
                            if (((context != null) && (expression != null)) && (context.Expression == expression))
                            {
                                obj2     = context.Value;
                                typeName = TypeDescriptor.GetClassName(obj2);
                            }
                        }
                    }
                }
                else if (obj2 == null)
                {
                    IContainer service = (IContainer)manager.GetService(typeof(IContainer));
                    if (service != null)
                    {
                        IComponent component = service.Components[name];
                        if (component != null)
                        {
                            typeName = component.GetType().FullName;
                            this._nameTable[name] = component;
                        }
                    }
                }
                if (typeName == null)
                {
                    return(obj2);
                }
                Type valueType = manager.GetType(typeName);
                if (valueType == null)
                {
                    manager.ReportError(new CodeDomSerializerException(System.Design.SR.GetString("SerializerTypeNotFound", new object[] { typeName }), manager));
                    return(obj2);
                }
                if ((statements == null) && this._statementTable.ContainsKey(name))
                {
                    statements = this._statementTable[name];
                }
                if ((statements == null) || (statements.Count <= 0))
                {
                    return(obj2);
                }
                CodeDomSerializer serializer = base.GetSerializer(manager, valueType);
                if (serializer == null)
                {
                    manager.ReportError(new CodeDomSerializerException(System.Design.SR.GetString("SerializerNoSerializerForComponent", new object[] { valueType.FullName }), manager));
                    return(obj2);
                }
                try
                {
                    obj2 = serializer.Deserialize(manager, statements);
                    if ((obj2 != null) && (field != null))
                    {
                        PropertyDescriptor descriptor = TypeDescriptor.GetProperties(obj2)["Modifiers"];
                        if ((descriptor != null) && (descriptor.PropertyType == typeof(MemberAttributes)))
                        {
                            MemberAttributes attributes = field.Attributes & MemberAttributes.AccessMask;
                            descriptor.SetValue(obj2, attributes);
                        }
                    }
                    this._nameTable[name] = obj2;
                }
                catch (Exception exception)
                {
                    manager.ReportError(exception);
                }
            }
            return(obj2);
        }
        private object DeserializeName(IDesignerSerializationManager manager, string name)
        {
            string typeName   = null;
            Type   objectType = null;
            object obj2       = this.nameTable[name];

            using (CodeDomSerializerBase.TraceScope("RootCodeDomSerializer::DeserializeName"))
            {
                CodeMemberField field = null;
                CodeObject      obj3  = obj2 as CodeObject;
                if (obj3 != null)
                {
                    obj2 = null;
                    this.nameTable[name] = null;
                    if (obj3 is CodeVariableDeclarationStatement)
                    {
                        CodeVariableDeclarationStatement statement = (CodeVariableDeclarationStatement)obj3;
                        typeName = CodeDomSerializerBase.GetTypeNameFromCodeTypeReference(manager, statement.Type);
                    }
                    else if (obj3 is CodeMemberField)
                    {
                        field    = (CodeMemberField)obj3;
                        typeName = CodeDomSerializerBase.GetTypeNameFromCodeTypeReference(manager, field.Type);
                    }
                }
                else
                {
                    if (obj2 != null)
                    {
                        return(obj2);
                    }
                    IContainer service = (IContainer)manager.GetService(typeof(IContainer));
                    if (service != null)
                    {
                        IComponent component = service.Components[name];
                        if (component != null)
                        {
                            typeName             = component.GetType().FullName;
                            this.nameTable[name] = component;
                        }
                    }
                }
                if (name.Equals(this.ContainerName))
                {
                    IContainer container2 = (IContainer)manager.GetService(typeof(IContainer));
                    if (container2 != null)
                    {
                        obj2 = container2;
                    }
                }
                else if (typeName != null)
                {
                    objectType = manager.GetType(typeName);
                    if (objectType == null)
                    {
                        manager.ReportError(new SerializationException(System.Design.SR.GetString("SerializerTypeNotFound", new object[] { typeName })));
                    }
                    else
                    {
                        CodeStatementCollection codeObject = (CodeStatementCollection)this.statementTable[name];
                        if ((codeObject != null) && (codeObject.Count > 0))
                        {
                            CodeDomSerializer serializer = (CodeDomSerializer)manager.GetSerializer(objectType, typeof(CodeDomSerializer));
                            if (serializer == null)
                            {
                                manager.ReportError(System.Design.SR.GetString("SerializerNoSerializerForComponent", new object[] { objectType.FullName }));
                            }
                            else
                            {
                                try
                                {
                                    obj2 = serializer.Deserialize(manager, codeObject);
                                    if ((obj2 != null) && (field != null))
                                    {
                                        PropertyDescriptor descriptor = TypeDescriptor.GetProperties(obj2)["Modifiers"];
                                        if ((descriptor != null) && (descriptor.PropertyType == typeof(MemberAttributes)))
                                        {
                                            MemberAttributes attributes = field.Attributes & MemberAttributes.AccessMask;
                                            descriptor.SetValue(obj2, attributes);
                                        }
                                    }
                                }
                                catch (Exception exception)
                                {
                                    manager.ReportError(exception);
                                }
                            }
                        }
                    }
                }
                this.nameTable[name] = obj2;
            }
            return(obj2);
        }
Ejemplo n.º 5
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);
		}