public SerializableTypeCodeDomSerializer(CodeDomSerializer originalSerializer)
        {
            if (originalSerializer == null)
                throw new ArgumentNullException("originalSerializer");

            this.originalSerializer = originalSerializer;
        }
            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);
            }
Esempio n. 3
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);
        }
Esempio n. 4
0
        /// <summary>
        ///  This method actually performs the serialization.  When the member is serialized
        ///  the necessary statements will be added to the statements collection.
        /// </summary>
        public override void Serialize(IDesignerSerializationManager manager, object value, MemberDescriptor descriptor, CodeStatementCollection statements)
        {
            if (manager is null)
            {
                throw new ArgumentNullException(nameof(manager));
            }
            if (value is null)
            {
                throw new ArgumentNullException(nameof(value));
            }
            if (!(descriptor is EventDescriptor eventToSerialize))
            {
                throw new ArgumentNullException(nameof(descriptor));
            }
            if (statements is null)
            {
                throw new ArgumentNullException(nameof(statements));
            }

            try
            {
                // If the IEventBindingService is not available, we don't throw - we just don't do anything.
                if (manager.GetService(typeof(IEventBindingService)) is IEventBindingService eventBindings)
                {
                    PropertyDescriptor prop       = eventBindings.GetEventProperty(eventToSerialize);
                    string             methodName = (string)prop.GetValue(value);

                    if (methodName != null)
                    {
                        CodeDomSerializer.Trace("Event {0} bound to {1}", eventToSerialize.Name, methodName);
                        CodeExpression eventTarget = SerializeToExpression(manager, value);
                        CodeDomSerializer.TraceWarningIf(eventTarget is null, "Object has no name and no propery ref in context so we cannot serialize events: {0}", value);
                        if (eventTarget != null)
                        {
                            CodeTypeReference            delegateTypeRef = new CodeTypeReference(eventToSerialize.EventType);
                            CodeDelegateCreateExpression delegateCreate  = new CodeDelegateCreateExpression(delegateTypeRef, _thisRef, methodName);
                            CodeEventReferenceExpression eventRef        = new CodeEventReferenceExpression(eventTarget, eventToSerialize.Name);
                            CodeAttachEventStatement     attach          = new CodeAttachEventStatement(eventRef, delegateCreate);

                            attach.UserData[typeof(Delegate)] = eventToSerialize.EventType;
                            statements.Add(attach);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                // Since we usually go through reflection, don't
                // show what our engine does, show what caused
                // the problem.
                //
                if (e is TargetInvocationException)
                {
                    e = e.InnerException;
                }

                manager.ReportError(new CodeDomSerializerException(string.Format(SR.SerializerPropertyGenFailed, eventToSerialize.Name, e.Message), manager));
            }
        }
        /// <summary>
        ///  This method actually performs the serialization.  When the member is serialized
        ///  the necessary statements will be added to the statements collection.
        /// </summary>
        public override void Serialize(IDesignerSerializationManager manager, object value, MemberDescriptor descriptor, CodeStatementCollection statements)
        {
            if (manager is null)
            {
                throw new ArgumentNullException(nameof(manager));
            }

            if (value is null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (!(descriptor is PropertyDescriptor propertyToSerialize))
            {
                throw new ArgumentNullException(nameof(descriptor));
            }

            if (statements is null)
            {
                throw new ArgumentNullException(nameof(statements));
            }

            try
            {
                ExtenderProvidedPropertyAttribute exAttr = (ExtenderProvidedPropertyAttribute)propertyToSerialize.Attributes[typeof(ExtenderProvidedPropertyAttribute)];
                bool isExtender        = (exAttr != null && exAttr.Provider != null);
                bool serializeContents = propertyToSerialize.Attributes.Contains(DesignerSerializationVisibilityAttribute.Content);

                CodeDomSerializer.Trace("Serializing property {0}", propertyToSerialize.Name);
                if (serializeContents)
                {
                    SerializeContentProperty(manager, value, propertyToSerialize, isExtender, statements);
                }
                else if (isExtender)
                {
                    SerializeExtenderProperty(manager, value, propertyToSerialize, statements);
                }
                else
                {
                    SerializeNormalProperty(manager, value, propertyToSerialize, statements);
                }
            }
            catch (Exception e)
            {
                // Since we usually go through reflection, don't
                // show what our engine does, show what caused
                // the problem.
                if (e is TargetInvocationException)
                {
                    e = e.InnerException;
                }

                manager.ReportError(new CodeDomSerializerException(string.Format(SR.SerializerPropertyGenFailed, propertyToSerialize.Name, e.Message), manager));
            }
        }
 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();
 }
		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 ();
		}
 private void ClearDocument()
 {
     if (this._documentType != null)
     {
         base.LoaderHost.RemoveService(typeof(CodeTypeDeclaration));
         this._documentType = null;
         this._documentNamespace = null;
         this._documentCompileUnit = null;
         this._rootSerializer = null;
         this._typeSerializer = null;
     }
 }
        private void SerializeContentProperty(IDesignerSerializationManager manager, object instance,
                                              PropertyDescriptor descriptor, CodeStatementCollection statements)
        {
            CodePropertyReferenceExpression propRef = new CodePropertyReferenceExpression();

            propRef.PropertyName = descriptor.Name;
            object propertyValue = descriptor.GetValue(instance);

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

            if (expressionCtx != null && expressionCtx.PresetValue == instance)
            {
                propRef.TargetObject = expressionCtx.Expression;
            }
            else
            {
                propRef.TargetObject = base.SerializeToExpression(manager, instance);
            }

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

            if (propRef.TargetObject != null && serializer != null)
            {
                manager.Context.Push(new ExpressionContext(propRef, propRef.GetType(), null, propertyValue));
                object serialized = serializer.Serialize(manager, propertyValue);
                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));
                }
            }
        }
Esempio n. 10
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);
                }
            }
        }
Esempio n. 11
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 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;
                session.Dispose();
            }
        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);
        }
		internal ProjectResourcesComponentCodeDomSerializer(CodeDomSerializer baseSerializer)
		{
			if (baseSerializer == null)
				throw new ArgumentNullException("baseSerializer");
			this.baseSerializer = baseSerializer;
		}
        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);
        }
        /// <summary>
        ///  This serializes the given property on this object.
        /// </summary>
        private void SerializeNormalProperty(IDesignerSerializationManager manager, object value, PropertyDescriptor property, CodeStatementCollection statements)
        {
            using (CodeDomSerializer.TraceScope("CodeDomSerializer::" + nameof(SerializeProperty)))
            {
                CodeExpression target = SerializeToExpression(manager, value);

                CodeDomSerializer.TraceWarningIf(target is null, "Unable to serialize target for property {0}", property.Name);
                if (target != null)
                {
                    CodeExpression propertyRef = new CodePropertyReferenceExpression(target, property.Name);

                    CodeExpression serializedPropertyValue = null;

                    // First check for a member relationship service to see if this property
                    // is related to another member.  If it is, then we will use that
                    // relationship to construct the property assign statement.  if
                    // it isn't, then we're serialize ourselves.

                    if (manager.GetService(typeof(MemberRelationshipService)) is MemberRelationshipService relationships)
                    {
                        MemberRelationship relationship = relationships[value, property];

                        if (relationship != MemberRelationship.Empty)
                        {
                            CodeExpression rhsTarget = SerializeToExpression(manager, relationship.Owner);

                            if (rhsTarget != null)
                            {
                                serializedPropertyValue = new CodePropertyReferenceExpression(rhsTarget, relationship.Member.Name);
                            }
                        }
                    }

                    if (serializedPropertyValue is null)
                    {
                        // Serialize the value of this property into a code expression.  If we can't get one,
                        // then we won't serialize the property.
                        //
                        object propValue = GetPropertyValue(manager, property, value, out bool validValue);

                        if (validValue)
                        {
                            ExpressionContext tree = null;

                            if (propValue != value)
                            {
                                // make sure the value isn't the object or we'll end up printing
                                // this property instead of the value.
                                tree = new ExpressionContext(propertyRef, property.PropertyType, value);
                                manager.Context.Push(tree);
                            }

                            try
                            {
                                serializedPropertyValue = SerializeToExpression(manager, propValue);
                            }
                            finally
                            {
                                if (tree != null)
                                {
                                    Debug.Assert(manager.Context.Current == tree, "Context stack corrupted.");
                                    manager.Context.Pop();
                                }
                            }
                        }
                    }

                    if (serializedPropertyValue != null)
                    {
                        CodeAssignStatement assign = new CodeAssignStatement(propertyRef, serializedPropertyValue);
                        statements.Add(assign);
                    }
                }
            }
        }
 internal LocalizationCodeDomSerializer(CodeDomLocalizationModel model, object currentSerializer)
 {
     this._model = model;
     this._currentSerializer = currentSerializer as CodeDomSerializer;
 }
 private void EnsureDocument(IDesignerSerializationManager manager)
 {
     if (this._documentCompileUnit == null)
     {
         this._documentCompileUnit = this.Parse();
         if (this._documentCompileUnit == null)
         {
             Exception exception = new NotSupportedException(System.Design.SR.GetString("CodeDomDesignerLoaderNoLanguageSupport")) {
                 HelpLink = "CodeDomDesignerLoaderNoLanguageSupport"
             };
             throw exception;
         }
     }
     if (this._documentType == null)
     {
         ArrayList list = null;
         bool flag = true;
         if (this._documentCompileUnit.UserData[typeof(InvalidOperationException)] != null)
         {
             InvalidOperationException exception2 = this._documentCompileUnit.UserData[typeof(InvalidOperationException)] as InvalidOperationException;
             if (exception2 != null)
             {
                 this._documentCompileUnit = null;
                 throw exception2;
             }
         }
         foreach (CodeNamespace namespace2 in this._documentCompileUnit.Namespaces)
         {
             foreach (CodeTypeDeclaration declaration in namespace2.Types)
             {
                 Type componentType = null;
                 foreach (CodeTypeReference reference in declaration.BaseTypes)
                 {
                     Type type = base.LoaderHost.GetType(CodeDomSerializerBase.GetTypeNameFromCodeTypeReference(manager, reference));
                     if ((type != null) && !type.IsInterface)
                     {
                         componentType = type;
                         break;
                     }
                     if (type == null)
                     {
                         if (list == null)
                         {
                             list = new ArrayList();
                         }
                         list.Add(System.Design.SR.GetString("CodeDomDesignerLoaderDocumentFailureTypeNotFound", new object[] { declaration.Name, reference.BaseType }));
                     }
                 }
                 if (componentType != null)
                 {
                     bool flag2 = false;
                     foreach (Attribute attribute in TypeDescriptor.GetAttributes(componentType))
                     {
                         if (attribute is RootDesignerSerializerAttribute)
                         {
                             RootDesignerSerializerAttribute attribute2 = (RootDesignerSerializerAttribute) attribute;
                             string serializerBaseTypeName = attribute2.SerializerBaseTypeName;
                             if ((serializerBaseTypeName != null) && (base.LoaderHost.GetType(serializerBaseTypeName) == typeof(CodeDomSerializer)))
                             {
                                 Type type3 = base.LoaderHost.GetType(attribute2.SerializerTypeName);
                                 if ((type3 != null) && (type3 != typeof(RootCodeDomSerializer)))
                                 {
                                     flag2 = true;
                                     if (!flag)
                                     {
                                         throw new InvalidOperationException(System.Design.SR.GetString("CodeDomDesignerLoaderSerializerTypeNotFirstType", new object[] { declaration.Name }));
                                     }
                                     this._rootSerializer = (CodeDomSerializer) Activator.CreateInstance(type3, BindingFlags.CreateInstance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, null, null);
                                     break;
                                 }
                             }
                         }
                     }
                     if ((this._rootSerializer == null) && this.HasRootDesignerAttribute(componentType))
                     {
                         this._typeSerializer = manager.GetSerializer(componentType, typeof(TypeCodeDomSerializer)) as TypeCodeDomSerializer;
                         if (!flag && (this._typeSerializer != null))
                         {
                             this._typeSerializer = null;
                             this._documentCompileUnit = null;
                             throw new InvalidOperationException(System.Design.SR.GetString("CodeDomDesignerLoaderSerializerTypeNotFirstType", new object[] { declaration.Name }));
                         }
                     }
                     if ((this._rootSerializer == null) && (this._typeSerializer == null))
                     {
                         if (list == null)
                         {
                             list = new ArrayList();
                         }
                         if (flag2)
                         {
                             list.Add(System.Design.SR.GetString("CodeDomDesignerLoaderDocumentFailureTypeDesignerNotInstalled", new object[] { declaration.Name, componentType.FullName }));
                         }
                         else
                         {
                             list.Add(System.Design.SR.GetString("CodeDomDesignerLoaderDocumentFailureTypeNotDesignable", new object[] { declaration.Name, componentType.FullName }));
                         }
                     }
                 }
                 if ((this._rootSerializer != null) || (this._typeSerializer != null))
                 {
                     this._documentNamespace = namespace2;
                     this._documentType = declaration;
                     break;
                 }
                 flag = false;
             }
             if (this._documentType != null)
             {
                 break;
             }
         }
         if (this._documentType == null)
         {
             Exception exception3;
             this._documentCompileUnit = null;
             if (list != null)
             {
                 StringBuilder builder = new StringBuilder();
                 foreach (string str2 in list)
                 {
                     builder.Append("\r\n");
                     builder.Append(str2);
                 }
                 exception3 = new InvalidOperationException(System.Design.SR.GetString("CodeDomDesignerLoaderNoRootSerializerWithFailures", new object[] { builder.ToString() })) {
                     HelpLink = "CodeDomDesignerLoaderNoRootSerializer"
                 };
             }
             else
             {
                 exception3 = new InvalidOperationException(System.Design.SR.GetString("CodeDomDesignerLoaderNoRootSerializer")) {
                     HelpLink = "CodeDomDesignerLoaderNoRootSerializer"
                 };
             }
             throw exception3;
         }
         base.LoaderHost.AddService(typeof(CodeTypeDeclaration), this._documentType);
     }
     codemarkers.CodeMarker(CodeMarkerEvent.perfFXGetDocumentType);
 }
        /// <summary>
        ///  This serializes the given property on this object as a content property.
        /// </summary>
        private void SerializeContentProperty(IDesignerSerializationManager manager, object value, PropertyDescriptor property, bool isExtender, CodeStatementCollection statements)
        {
            CodeDomSerializer.Trace("Property is marked as Visibility.Content.  Recursing.");

            object propertyValue = GetPropertyValue(manager, property, value, out bool validValue);

            // For persist contents objects, we don't just serialize the properties on the object; we
            // serialize everything.
            //
            CodeDomSerializer serializer = null;

            if (propertyValue is null)
            {
                CodeDomSerializer.TraceError("Property {0} is marked as Visibility.Content but it is returning null.", property.Name);

                string name = manager.GetName(value);

                if (name is null)
                {
                    name = value.GetType().FullName;
                }

                manager.ReportError(new CodeDomSerializerException(string.Format(SR.SerializerNullNestedProperty, name, property.Name), manager));
            }
            else
            {
                serializer = (CodeDomSerializer)manager.GetSerializer(propertyValue.GetType(), typeof(CodeDomSerializer));
                if (serializer != null)
                {
                    // Create a property reference expression and push it on the context stack.
                    // This allows the serializer to gain some context as to what it should be
                    // serializing.
                    CodeExpression target = SerializeToExpression(manager, value);

                    if (target is null)
                    {
                        CodeDomSerializer.TraceWarning("Unable to convert value to expression object");
                    }
                    else
                    {
                        CodeExpression propertyRef = null;

                        if (isExtender)
                        {
                            CodeDomSerializer.Trace("Content property is an extender.");
                            ExtenderProvidedPropertyAttribute exAttr = (ExtenderProvidedPropertyAttribute)property.Attributes[typeof(ExtenderProvidedPropertyAttribute)];

                            // Extender properties are method invokes on a target "extender" object.
                            //
                            CodeExpression extender = SerializeToExpression(manager, exAttr.Provider);
                            CodeExpression extended = SerializeToExpression(manager, value);

                            CodeDomSerializer.TraceWarningIf(extender is null, "Extender object {0} could not be serialized.", manager.GetName(exAttr.Provider));
                            CodeDomSerializer.TraceWarningIf(extended is null, "Extended object {0} could not be serialized.", manager.GetName(value));
                            if (extender != null && extended != null)
                            {
                                CodeMethodReferenceExpression methodRef    = new CodeMethodReferenceExpression(extender, "Get" + property.Name);
                                CodeMethodInvokeExpression    methodInvoke = new CodeMethodInvokeExpression
                                {
                                    Method = methodRef
                                };
                                methodInvoke.Parameters.Add(extended);
                                propertyRef = methodInvoke;
                            }
                        }
                        else
                        {
                            propertyRef = new CodePropertyReferenceExpression(target, property.Name);
                        }

                        if (propertyRef != null)
                        {
                            ExpressionContext tree = new ExpressionContext(propertyRef, property.PropertyType, value, propertyValue);
                            manager.Context.Push(tree);

                            object result = null;

                            try
                            {
                                SerializeAbsoluteContext absolute = (SerializeAbsoluteContext)manager.Context[typeof(SerializeAbsoluteContext)];

                                if (IsSerialized(manager, propertyValue, absolute != null))
                                {
                                    result = GetExpression(manager, propertyValue);
                                }
                                else
                                {
                                    result = serializer.Serialize(manager, propertyValue);
                                }
                            }
                            finally
                            {
                                Debug.Assert(manager.Context.Current == tree, "Serializer added a context it didn't remove.");
                                manager.Context.Pop();
                            }

                            if (result is CodeStatementCollection csc)
                            {
                                foreach (CodeStatement statement in csc)
                                {
                                    statements.Add(statement);
                                }
                            }
                            else
                            {
                                if (result is CodeStatement cs)
                                {
                                    statements.Add(cs);
                                }
                            }
                        }
                    }
                }
                else
                {
                    CodeDomSerializer.TraceError("Property {0} is marked as Visibilty.Content but there is no serializer for it.", property.Name);

                    manager.ReportError(new CodeDomSerializerException(string.Format(SR.SerializerNoSerializerForComponent, property.PropertyType.FullName), manager));
                }
            }
        }
        /// <summary>
        ///  This serializes the given property on this object.
        /// </summary>
        private void SerializeExtenderProperty(IDesignerSerializationManager manager, object value, PropertyDescriptor property, CodeStatementCollection statements)
        {
            AttributeCollection attributes = property.Attributes;

            using (CodeDomSerializer.TraceScope("PropertyMemberCodeDomSerializer::" + nameof(SerializeExtenderProperty)))
            {
                ExtenderProvidedPropertyAttribute exAttr = (ExtenderProvidedPropertyAttribute)attributes[typeof(ExtenderProvidedPropertyAttribute)];

                // Extender properties are method invokes on a target "extender" object.
                //
                CodeExpression extender = SerializeToExpression(manager, exAttr.Provider);
                CodeExpression extended = SerializeToExpression(manager, value);

                CodeDomSerializer.TraceWarningIf(extender is null, "Extender object {0} could not be serialized.", manager.GetName(exAttr.Provider));
                CodeDomSerializer.TraceWarningIf(extended is null, "Extended object {0} could not be serialized.", manager.GetName(value));
                if (extender != null && extended != null)
                {
                    CodeMethodReferenceExpression methodRef = new CodeMethodReferenceExpression(extender, "Set" + property.Name);
                    object         propValue = GetPropertyValue(manager, property, value, out bool validValue);
                    CodeExpression serializedPropertyValue = null;

                    // Serialize the value of this property into a code expression.  If we can't get one,
                    // then we won't serialize the property.
                    if (validValue)
                    {
                        ExpressionContext tree = null;

                        if (propValue != value)
                        {
                            // make sure the value isn't the object or we'll end up printing
                            // this property instead of the value.
                            tree = new ExpressionContext(methodRef, property.PropertyType, value);
                            manager.Context.Push(tree);
                        }

                        try
                        {
                            serializedPropertyValue = SerializeToExpression(manager, propValue);
                        }
                        finally
                        {
                            if (tree != null)
                            {
                                Debug.Assert(manager.Context.Current == tree, "Context stack corrupted.");
                                manager.Context.Pop();
                            }
                        }
                    }

                    if (serializedPropertyValue != null)
                    {
                        CodeMethodInvokeExpression methodInvoke = new CodeMethodInvokeExpression
                        {
                            Method = methodRef
                        };
                        methodInvoke.Parameters.Add(extended);
                        methodInvoke.Parameters.Add(serializedPropertyValue);
                        statements.Add(methodInvoke);
                    }
                }
            }
        }
Esempio n. 21
0
 /// <summary>
 ///  Only we can create an instance of this. Everyone else accesses it though
 ///  static properties.
 /// </summary>
 internal LocalizationCodeDomSerializer(CodeDomLocalizationModel model, object currentSerializer)
 {
     _model             = model;
     _currentSerializer = currentSerializer as CodeDomSerializer;
 }
Esempio n. 22
0
        private void SerializeContentProperty(IDesignerSerializationManager manager, object value, PropertyDescriptor property, bool isExtender, CodeStatementCollection statements)
        {
            bool              flag;
            object            presetValue = this.GetPropertyValue(manager, property, value, out flag);
            CodeDomSerializer serializer  = null;

            if (presetValue == null)
            {
                string name = manager.GetName(value);
                if (name == null)
                {
                    name = value.GetType().FullName;
                }
                manager.ReportError(System.Design.SR.GetString("SerializerNullNestedProperty", new object[] { name, property.Name }));
            }
            else
            {
                serializer = (CodeDomSerializer)manager.GetSerializer(presetValue.GetType(), typeof(CodeDomSerializer));
                if (serializer != null)
                {
                    CodeExpression targetObject = base.SerializeToExpression(manager, value);
                    if (targetObject != null)
                    {
                        CodeExpression expression = null;
                        if (isExtender)
                        {
                            ExtenderProvidedPropertyAttribute attribute = (ExtenderProvidedPropertyAttribute)property.Attributes[typeof(ExtenderProvidedPropertyAttribute)];
                            CodeExpression expression3 = base.SerializeToExpression(manager, attribute.Provider);
                            CodeExpression expression4 = base.SerializeToExpression(manager, value);
                            if ((expression3 != null) && (expression4 != null))
                            {
                                CodeMethodReferenceExpression expression5 = new CodeMethodReferenceExpression(expression3, "Get" + property.Name);
                                CodeMethodInvokeExpression    expression6 = new CodeMethodInvokeExpression {
                                    Method = expression5
                                };
                                expression6.Parameters.Add(expression4);
                                expression = expression6;
                            }
                        }
                        else
                        {
                            expression = new CodePropertyReferenceExpression(targetObject, property.Name);
                        }
                        if (expression != null)
                        {
                            ExpressionContext context = new ExpressionContext(expression, property.PropertyType, value, presetValue);
                            manager.Context.Push(context);
                            object obj3 = null;
                            try
                            {
                                SerializeAbsoluteContext context2 = (SerializeAbsoluteContext)manager.Context[typeof(SerializeAbsoluteContext)];
                                if (base.IsSerialized(manager, presetValue, context2 != null))
                                {
                                    obj3 = base.GetExpression(manager, presetValue);
                                }
                                else
                                {
                                    obj3 = serializer.Serialize(manager, presetValue);
                                }
                            }
                            finally
                            {
                                manager.Context.Pop();
                            }
                            CodeStatementCollection statements2 = obj3 as CodeStatementCollection;
                            if (statements2 == null)
                            {
                                CodeStatement statement2 = obj3 as CodeStatement;
                                if (statement2 != null)
                                {
                                    statements.Add(statement2);
                                }
                            }
                            else
                            {
                                foreach (CodeStatement statement in statements2)
                                {
                                    statements.Add(statement);
                                }
                            }
                        }
                    }
                }
                else
                {
                    manager.ReportError(System.Design.SR.GetString("SerializerNoSerializerForComponent", new object[] { property.PropertyType.FullName }));
                }
            }
        }