コード例 #1
0
 public void WriteResourceInvariant(IDesignerSerializationManager manager, string name, object value)
 {
     using (CodeDomSerializerBase.TraceScope("ResourceCodeDomSerializer::WriteResourceInvariant"))
     {
         this.GetResourceManager(manager).SetValue(manager, name, value, true, true, true, false);
     }
 }
コード例 #2
0
 public void SerializeMetadata(IDesignerSerializationManager manager, string name, object value, bool shouldSerializeValue)
 {
     using (CodeDomSerializerBase.TraceScope("ResourceCodeDomSerializer::SerializeMetadata"))
     {
         this.GetResourceManager(manager).SetMetadata(manager, name, value, shouldSerializeValue, false);
     }
 }
 private Type[] GetContainerConstructor(IDesignerSerializationManager manager)
 {
     if (this._containerConstructor == null)
     {
         this._containerConstructor = new Type[] { CodeDomSerializerBase.GetReflectionTypeFromTypeHelper(manager, typeof(IContainer)) };
     }
     return(this._containerConstructor);
 }
コード例 #4
0
        public override object Serialize(IDesignerSerializationManager manager, object value)
        {
            PropertyDescriptor descriptor = (PropertyDescriptor)manager.Context[typeof(PropertyDescriptor)];
            ExpressionContext  context    = (ExpressionContext)manager.Context[typeof(ExpressionContext)];
            bool flag  = (value != null) ? CodeDomSerializerBase.GetReflectionTypeHelper(manager, value).IsSerializable : true;
            bool flag2 = !flag;
            bool flag3 = (descriptor != null) && descriptor.Attributes.Contains(DesignerSerializationVisibilityAttribute.Content);

            if (!flag2)
            {
                flag2 = ((context != null) && (context.PresetValue != null)) && (context.PresetValue == value);
            }
            if (((this._model == CodeDomLocalizationModel.PropertyReflection) && !flag3) && !flag2)
            {
                CodeStatementCollection statements = (CodeStatementCollection)manager.Context[typeof(CodeStatementCollection)];
                bool flag4 = false;
                ExtenderProvidedPropertyAttribute attribute = null;
                if (descriptor != null)
                {
                    attribute = descriptor.Attributes[typeof(ExtenderProvidedPropertyAttribute)] as ExtenderProvidedPropertyAttribute;
                    if ((attribute != null) && (attribute.ExtenderProperty != null))
                    {
                        flag4 = true;
                    }
                }
                if ((!flag4 && (context != null)) && (statements != null))
                {
                    string         name       = manager.GetName(context.Owner);
                    CodeExpression expression = base.SerializeToExpression(manager, context.Owner);
                    if ((name != null) && (expression != null))
                    {
                        RootContext context2 = manager.Context[typeof(RootContext)] as RootContext;
                        if ((context2 != null) && (context2.Value == context.Owner))
                        {
                            name = "$this";
                        }
                        base.SerializeToResourceExpression(manager, value, false);
                        if (this.EmitApplyMethod(manager, context.Owner))
                        {
                            ResourceManager manager2 = manager.Context[typeof(ResourceManager)] as ResourceManager;
                            CodeMethodReferenceExpression expression3 = new CodeMethodReferenceExpression(base.GetExpression(manager, manager2), "ApplyResources");
                            CodeMethodInvokeExpression    expression4 = new CodeMethodInvokeExpression {
                                Method = expression3
                            };
                            expression4.Parameters.Add(expression);
                            expression4.Parameters.Add(new CodePrimitiveExpression(name));
                            statements.Add(expression4);
                        }
                        return(null);
                    }
                }
            }
            if (flag2)
            {
                return(this._currentSerializer.Serialize(manager, value));
            }
            return(base.SerializeToResourceExpression(manager, value));
        }
コード例 #5
0
 private void SerializeNormalProperty(IDesignerSerializationManager manager, object value, PropertyDescriptor property, CodeStatementCollection statements)
 {
     using (CodeDomSerializerBase.TraceScope("CodeDomSerializer::SerializeProperty"))
     {
         CodeExpression targetObject = base.SerializeToExpression(manager, value);
         if (targetObject != null)
         {
             CodeExpression            expression = new CodePropertyReferenceExpression(targetObject, property.Name);
             CodeExpression            right      = null;
             MemberRelationshipService service    = manager.GetService(typeof(MemberRelationshipService)) as MemberRelationshipService;
             if (service != null)
             {
                 MemberRelationship relationship = service[value, property];
                 if (relationship != MemberRelationship.Empty)
                 {
                     CodeExpression expression4 = base.SerializeToExpression(manager, relationship.Owner);
                     if (expression4 != null)
                     {
                         right = new CodePropertyReferenceExpression(expression4, relationship.Member.Name);
                     }
                 }
             }
             if (right == null)
             {
                 bool   flag;
                 object obj2 = this.GetPropertyValue(manager, property, value, out flag);
                 if (flag)
                 {
                     ExpressionContext context = null;
                     if (obj2 != value)
                     {
                         context = new ExpressionContext(expression, property.PropertyType, value);
                         manager.Context.Push(context);
                     }
                     try
                     {
                         right = base.SerializeToExpression(manager, obj2);
                     }
                     finally
                     {
                         if (context != null)
                         {
                             manager.Context.Pop();
                         }
                     }
                 }
             }
             if (right != null)
             {
                 CodeAssignStatement statement = new CodeAssignStatement(expression, right);
                 statements.Add(statement);
             }
         }
     }
 }
コード例 #6
0
 private void OnResolveName(object sender, ResolveNameEventArgs e)
 {
     using (CodeDomSerializerBase.TraceScope("RootCodeDomSerializer::OnResolveName"))
     {
         if (e.Value == null)
         {
             IDesignerSerializationManager manager = (IDesignerSerializationManager)sender;
             e.Value = this.DeserializeName(manager, e.Name, null);
         }
     }
 }
コード例 #7
0
        public object GetSerializer(IDesignerSerializationManager manager, object currentSerializer,
                                    Type objectType, Type serializerType)
        {
            CodeDomSerializerBase serializer = null;

            if (serializerType == typeof(CodeDomSerializer)) // CodeDomSerializer
            {
                if (objectType == null)                      // means that value to serialize is null CodePrimitiveExpression (null)
                {
                    serializer = _primitiveSerializer;
                }
                else if (typeof(IComponent).IsAssignableFrom(objectType))
                {
                    serializer = _componentSerializer;
                }
                else if (objectType.IsEnum || typeof(Enum).IsAssignableFrom(objectType))
                {
                    serializer = _enumSerializer;
                }
                else if (objectType.IsPrimitive || objectType == typeof(String))
                {
                    serializer = _primitiveSerializer;
                }
                else if (typeof(ICollection).IsAssignableFrom(objectType))
                {
                    serializer = _collectionSerializer;
                }
                else
                {
                    serializer = _othersSerializer;
                }
            }
            else if (serializerType == typeof(MemberCodeDomSerializer))                 // MemberCodeDomSerializer
            {
                if (typeof(PropertyDescriptor).IsAssignableFrom(objectType))
                {
                    serializer = _propertySerializer;
                }
                else if (typeof(EventDescriptor).IsAssignableFrom(objectType))
                {
                    serializer = _eventSerializer;
                }
            }
            else if (serializerType == typeof(RootCodeDomSerializer))
            {
                serializer = _rootSerializer;
            }

            return(serializer);
        }
コード例 #8
0
        public override object Serialize(IDesignerSerializationManager manager, object value)
        {
            CodeExpression left = null;

            using (CodeDomSerializerBase.TraceScope("EnumCodeDomSerializer::Serialize"))
            {
                Enum[] enumArray;
                if (!(value is Enum))
                {
                    return(left);
                }
                bool          flag      = false;
                TypeConverter converter = TypeDescriptor.GetConverter(value);
                if ((converter != null) && converter.CanConvertTo(typeof(Enum[])))
                {
                    enumArray = (Enum[])converter.ConvertTo(value, typeof(Enum[]));
                    flag      = enumArray.Length > 1;
                }
                else
                {
                    enumArray = new Enum[] { (Enum)value };
                    flag      = true;
                }
                CodeTypeReferenceExpression targetObject = new CodeTypeReferenceExpression(value.GetType());
                TypeConverter converter2 = new EnumConverter(value.GetType());
                foreach (Enum enum2 in enumArray)
                {
                    string         str   = (converter2 != null) ? converter2.ConvertToString(enum2) : null;
                    CodeExpression right = !string.IsNullOrEmpty(str) ? new CodeFieldReferenceExpression(targetObject, str) : null;
                    if (right != null)
                    {
                        if (left == null)
                        {
                            left = right;
                        }
                        else
                        {
                            left = new CodeBinaryOperatorExpression(left, CodeBinaryOperatorType.BitwiseOr, right);
                        }
                    }
                }
                if ((left != null) && flag)
                {
                    left = new CodeCastExpression(value.GetType(), left);
                }
            }
            return(left);
        }
コード例 #9
0
        public override object Deserialize(IDesignerSerializationManager manager, object codeObject)
        {
            object obj2 = null;

            if ((manager == null) || (codeObject == null))
            {
                throw new ArgumentNullException((manager == null) ? "manager" : "codeObject");
            }
            using (CodeDomSerializerBase.TraceScope("ResourceCodeDomSerializer::Deserialize"))
            {
                CodeExpression expression = codeObject as CodeExpression;
                if (expression != null)
                {
                    return(base.DeserializeExpression(manager, null, expression));
                }
                CodeStatementCollection statements = codeObject as CodeStatementCollection;
                if (statements != null)
                {
                    foreach (CodeStatement statement in statements)
                    {
                        if (statement is CodeVariableDeclarationStatement)
                        {
                            CodeVariableDeclarationStatement statement2 = (CodeVariableDeclarationStatement)statement;
                            if (statement2.Name.Equals(this.ResourceManagerName))
                            {
                                obj2 = this.CreateResourceManager(manager);
                            }
                        }
                        else if (obj2 == null)
                        {
                            obj2 = base.DeserializeStatementToInstance(manager, statement);
                        }
                        else
                        {
                            base.DeserializeStatement(manager, statement);
                        }
                    }
                    return(obj2);
                }
                if (!(codeObject is CodeStatement))
                {
                    string str = string.Format(CultureInfo.CurrentCulture, "{0}, {1}, {2}", new object[] { typeof(CodeExpression).Name, typeof(CodeStatement).Name, typeof(CodeStatementCollection).Name });
                    throw new ArgumentException(System.Design.SR.GetString("SerializerBadElementTypes", new object[] { codeObject.GetType().Name, str }));
                }
            }
            return(obj2);
        }
コード例 #10
0
        private void SerializeExtenderProperty(IDesignerSerializationManager manager, object value, PropertyDescriptor property, CodeStatementCollection statements)
        {
            AttributeCollection attributes = property.Attributes;

            using (CodeDomSerializerBase.TraceScope("PropertyMemberCodeDomSerializer::SerializeExtenderProperty"))
            {
                ExtenderProvidedPropertyAttribute attribute = (ExtenderProvidedPropertyAttribute)attributes[typeof(ExtenderProvidedPropertyAttribute)];
                CodeExpression targetObject = base.SerializeToExpression(manager, attribute.Provider);
                CodeExpression expression2  = base.SerializeToExpression(manager, value);
                if ((targetObject != null) && (expression2 != null))
                {
                    bool flag;
                    CodeMethodReferenceExpression expression = new CodeMethodReferenceExpression(targetObject, "Set" + property.Name);
                    object         obj2        = this.GetPropertyValue(manager, property, value, out flag);
                    CodeExpression expression4 = null;
                    if (flag)
                    {
                        ExpressionContext context = null;
                        if (obj2 != value)
                        {
                            context = new ExpressionContext(expression, property.PropertyType, value);
                            manager.Context.Push(context);
                        }
                        try
                        {
                            expression4 = base.SerializeToExpression(manager, obj2);
                        }
                        finally
                        {
                            if (context != null)
                            {
                                manager.Context.Pop();
                            }
                        }
                    }
                    if (expression4 != null)
                    {
                        CodeMethodInvokeExpression expression5 = new CodeMethodInvokeExpression {
                            Method = expression
                        };
                        expression5.Parameters.Add(expression2);
                        expression5.Parameters.Add(expression4);
                        statements.Add(expression5);
                    }
                }
            }
        }
コード例 #11
0
        private CodeStatementCollection SerializeRootObject(IDesignerSerializationManager manager, object value, bool designTime)
        {
            if (((CodeTypeDeclaration)manager.Context[typeof(CodeTypeDeclaration)]) == null)
            {
                return(null);
            }
            CodeStatementCollection statements = new CodeStatementCollection();

            using (CodeDomSerializerBase.TraceScope("RootCodeDomSerializer::SerializeRootObject"))
            {
                if (designTime)
                {
                    base.SerializeProperties(manager, statements, value, designTimeProperties);
                    return(statements);
                }
                base.SerializeProperties(manager, statements, value, runTimeProperties);
                base.SerializeEvents(manager, statements, value, null);
            }
            return(statements);
        }
コード例 #12
0
        protected CodeExpression SerializeToReferenceExpression(IDesignerSerializationManager manager, object value)
        {
            CodeExpression expression = null;

            using (CodeDomSerializerBase.TraceScope("CodeDomSerializer::SerializeToReferenceExpression"))
            {
                expression = base.GetExpression(manager, value);
                if ((expression != null) || !(value is IComponent))
                {
                    return(expression);
                }
                string name = manager.GetName(value);
                bool   flag = false;
                if (name == null)
                {
                    IReferenceService service = (IReferenceService)manager.GetService(typeof(IReferenceService));
                    if (service != null)
                    {
                        name = service.GetName(value);
                        flag = name != null;
                    }
                }
                if (name == null)
                {
                    return(expression);
                }
                RootContext context = (RootContext)manager.Context[typeof(RootContext)];
                if ((context != null) && (context.Value == value))
                {
                    return(context.Expression);
                }
                if (flag && (name.IndexOf('.') != -1))
                {
                    int index = name.IndexOf('.');
                    return(new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(_thisRef, name.Substring(0, index)), name.Substring(index + 1)));
                }
                return(new CodeFieldReferenceExpression(_thisRef, name));
            }
        }
コード例 #13
0
        public override object Serialize(IDesignerSerializationManager manager, object value)
        {
            using (CodeDomSerializerBase.TraceScope("PrimitiveCodeDomSerializer::Serialize"))
            {
            }
            CodeExpression expression = new CodePrimitiveExpression(value);

            if (value == null)
            {
                return(expression);
            }
            if (((value is bool) || (value is char)) || (((value is int) || (value is float)) || (value is double)))
            {
                CodeDomProvider service = manager.GetService(typeof(CodeDomProvider)) as CodeDomProvider;
                if ((service != null) && string.Equals(service.FileExtension, JSharpFileExtension))
                {
                    ExpressionContext context = manager.Context[typeof(ExpressionContext)] as ExpressionContext;
                    if ((context != null) && (context.ExpressionType == typeof(object)))
                    {
                        expression = new CodeCastExpression(value.GetType(), expression);
                        expression.UserData.Add("CastIsBoxing", true);
                    }
                }
                return(expression);
            }
            if (value is string)
            {
                string str = value as string;
                if ((str != null) && (str.Length > 200))
                {
                    expression = base.SerializeToResourceExpression(manager, str);
                }
                return(expression);
            }
            return(new CodeCastExpression(new CodeTypeReference(value.GetType()), expression));
        }
コード例 #14
0
        public override object Serialize(IDesignerSerializationManager manager, object value)
        {
            if (manager == null)
            {
                throw new ArgumentNullException("manager");
            }
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            object obj2 = null;

            using (CodeDomSerializerBase.TraceScope("CollectionCodeDomSerializer::Serialize"))
            {
                CodeExpression     expression;
                ExpressionContext  context    = manager.Context[typeof(ExpressionContext)] as ExpressionContext;
                PropertyDescriptor descriptor = manager.Context[typeof(PropertyDescriptor)] as PropertyDescriptor;
                if (((context != null) && (context.PresetValue == value)) && ((descriptor != null) && (descriptor.PropertyType == context.ExpressionType)))
                {
                    expression = context.Expression;
                }
                else
                {
                    expression = null;
                    context    = null;
                    descriptor = null;
                }
                ICollection is2 = value as ICollection;
                if (is2 == null)
                {
                    return(obj2);
                }
                ICollection valuesToSerialize           = is2;
                InheritedPropertyDescriptor descriptor2 = descriptor as InheritedPropertyDescriptor;
                Type c    = (context == null) ? is2.GetType() : context.ExpressionType;
                bool flag = typeof(Array).IsAssignableFrom(c);
                if ((expression == null) && !flag)
                {
                    bool flag2;
                    expression = base.SerializeCreationExpression(manager, is2, out flag2);
                    if (flag2)
                    {
                        return(expression);
                    }
                }
                if ((expression == null) && !flag)
                {
                    return(obj2);
                }
                if ((descriptor2 != null) && !flag)
                {
                    valuesToSerialize = this.GetCollectionDelta(descriptor2.OriginalValue as ICollection, is2);
                }
                obj2 = this.SerializeCollection(manager, expression, c, is2, valuesToSerialize);
                if ((expression == null) || !this.ShouldClearCollection(manager, is2))
                {
                    return(obj2);
                }
                CodeStatementCollection statements = obj2 as CodeStatementCollection;
                if ((is2.Count > 0) && ((obj2 == null) || ((statements != null) && (statements.Count == 0))))
                {
                    return(null);
                }
                if (statements == null)
                {
                    statements = new CodeStatementCollection();
                    CodeStatement statement = obj2 as CodeStatement;
                    if (statement != null)
                    {
                        statements.Add(statement);
                    }
                    obj2 = statements;
                }
                if (statements != null)
                {
                    CodeMethodInvokeExpression expression2 = new CodeMethodInvokeExpression(expression, "Clear", new CodeExpression[0]);
                    CodeExpressionStatement    statement2  = new CodeExpressionStatement(expression2);
                    statements.Insert(0, statement2);
                }
            }
            return(obj2);
        }
コード例 #15
0
        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);
        }
コード例 #16
0
        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);
        }
コード例 #17
0
        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);
        }
コード例 #18
0
        private void FillStatementTable(CodeMemberMethod method, string className)
        {
            using (CodeDomSerializerBase.TraceScope("RootCodeDomSerializer::FillStatementTable"))
            {
                foreach (CodeStatement statement in method.Statements)
                {
                    CodeExpression left = null;
                    if (statement is CodeAssignStatement)
                    {
                        left = ((CodeAssignStatement)statement).Left;
                    }
                    else if (statement is CodeAttachEventStatement)
                    {
                        left = ((CodeAttachEventStatement)statement).Event;
                    }
                    else if (statement is CodeRemoveEventStatement)
                    {
                        left = ((CodeRemoveEventStatement)statement).Event;
                    }
                    else if (statement is CodeExpressionStatement)
                    {
                        left = ((CodeExpressionStatement)statement).Expression;
                    }
                    else if (statement is CodeVariableDeclarationStatement)
                    {
                        CodeVariableDeclarationStatement statement2 = (CodeVariableDeclarationStatement)statement;
                        if ((statement2.InitExpression != null) && this.nameTable.Contains(statement2.Name))
                        {
                            this.AddStatement(statement2.Name, statement2);
                        }
                        left = null;
                    }
                    if (left == null)
                    {
                        continue;
                    }
Label_00C4:
                    while (left is CodeCastExpression)
                    {
                        left = ((CodeCastExpression)left).Expression;
                    }
                    if (left is CodeDelegateCreateExpression)
                    {
                        left = ((CodeDelegateCreateExpression)left).TargetObject;
                        goto Label_00C4;
                    }
                    if (left is CodeDelegateInvokeExpression)
                    {
                        left = ((CodeDelegateInvokeExpression)left).TargetObject;
                        goto Label_00C4;
                    }
                    if (left is CodeDirectionExpression)
                    {
                        left = ((CodeDirectionExpression)left).Expression;
                        goto Label_00C4;
                    }
                    if (left is CodeEventReferenceExpression)
                    {
                        left = ((CodeEventReferenceExpression)left).TargetObject;
                        goto Label_00C4;
                    }
                    if (left is CodeMethodInvokeExpression)
                    {
                        left = ((CodeMethodInvokeExpression)left).Method;
                        goto Label_00C4;
                    }
                    if (left is CodeMethodReferenceExpression)
                    {
                        left = ((CodeMethodReferenceExpression)left).TargetObject;
                        goto Label_00C4;
                    }
                    if (left is CodeArrayIndexerExpression)
                    {
                        left = ((CodeArrayIndexerExpression)left).TargetObject;
                        goto Label_00C4;
                    }
                    if (left is CodeFieldReferenceExpression)
                    {
                        CodeFieldReferenceExpression expression2 = (CodeFieldReferenceExpression)left;
                        if (expression2.TargetObject is CodeThisReferenceExpression)
                        {
                            this.AddStatement(expression2.FieldName, statement);
                            continue;
                        }
                        left = expression2.TargetObject;
                        goto Label_00C4;
                    }
                    if (left is CodePropertyReferenceExpression)
                    {
                        CodePropertyReferenceExpression expression3 = (CodePropertyReferenceExpression)left;
                        if ((expression3.TargetObject is CodeThisReferenceExpression) && this.nameTable.Contains(expression3.PropertyName))
                        {
                            this.AddStatement(expression3.PropertyName, statement);
                            continue;
                        }
                        left = expression3.TargetObject;
                        goto Label_00C4;
                    }
                    if (left is CodeVariableReferenceExpression)
                    {
                        CodeVariableReferenceExpression expression4 = (CodeVariableReferenceExpression)left;
                        if (this.nameTable.Contains(expression4.VariableName))
                        {
                            this.AddStatement(expression4.VariableName, statement);
                        }
                    }
                    else if ((left is CodeThisReferenceExpression) || (left is CodeBaseReferenceExpression))
                    {
                        this.AddStatement(className, statement);
                    }
                }
            }
        }
コード例 #19
0
        protected virtual object SerializeCollection(IDesignerSerializationManager manager, CodeExpression targetExpression, Type targetType, ICollection originalCollection, ICollection valuesToSerialize)
        {
            if (manager == null)
            {
                throw new ArgumentNullException("manager");
            }
            if (targetType == null)
            {
                throw new ArgumentNullException("targetType");
            }
            if (originalCollection == null)
            {
                throw new ArgumentNullException("originalCollection");
            }
            if (valuesToSerialize == null)
            {
                throw new ArgumentNullException("valuesToSerialize");
            }
            object obj2 = null;
            bool   flag = false;

            if (typeof(Array).IsAssignableFrom(targetType))
            {
                CodeArrayCreateExpression right = this.SerializeArray(manager, targetType, originalCollection, valuesToSerialize);
                if (right != null)
                {
                    if (targetExpression != null)
                    {
                        obj2 = new CodeAssignStatement(targetExpression, right);
                    }
                    else
                    {
                        obj2 = right;
                    }
                    flag = true;
                }
                return(obj2);
            }
            if (valuesToSerialize.Count > 0)
            {
                TypeDescriptionProvider targetFrameworkProvider = CodeDomSerializerBase.GetTargetFrameworkProvider(manager, originalCollection);
                if (targetFrameworkProvider == null)
                {
                    targetFrameworkProvider = TypeDescriptor.GetProvider(originalCollection);
                }
                MethodInfo[]      methods = targetFrameworkProvider.GetReflectionType(originalCollection).GetMethods(BindingFlags.Public | BindingFlags.Instance);
                List <MethodInfo> list    = new List <MethodInfo>();
                List <MethodInfo> list2   = new List <MethodInfo>();
                foreach (MethodInfo info in methods)
                {
                    if (info.Name.Equals("AddRange"))
                    {
                        ParameterInfo[] parameters = info.GetParameters();
                        if (((parameters.Length == 1) && parameters[0].ParameterType.IsArray) && this.MethodSupportsSerialization(info))
                        {
                            list.Add(info);
                        }
                    }
                    if ((info.Name.Equals("Add") && (info.GetParameters().Length == 1)) && this.MethodSupportsSerialization(info))
                    {
                        list2.Add(info);
                    }
                }
                MethodInfo info2 = ChooseMethodByType(targetFrameworkProvider, list, valuesToSerialize);
                if (info2 != null)
                {
                    Type runtimeType = targetFrameworkProvider.GetRuntimeType(info2.GetParameters()[0].ParameterType.GetElementType());
                    obj2 = this.SerializeViaAddRange(manager, targetExpression, targetType, runtimeType, valuesToSerialize);
                    flag = true;
                }
                else
                {
                    MethodInfo info3 = ChooseMethodByType(targetFrameworkProvider, list2, valuesToSerialize);
                    if (info3 != null)
                    {
                        Type elementType = targetFrameworkProvider.GetRuntimeType(info3.GetParameters()[0].ParameterType);
                        obj2 = this.SerializeViaAdd(manager, targetExpression, targetType, elementType, valuesToSerialize);
                        flag = true;
                    }
                }
                if (!flag && originalCollection.GetType().IsSerializable)
                {
                    obj2 = base.SerializeToResourceExpression(manager, originalCollection, false);
                }
            }
            return(obj2);
        }
コード例 #20
0
        private object SerializeViaAddRange(IDesignerSerializationManager manager, CodeExpression targetExpression, Type targetType, Type elementType, ICollection valuesToSerialize)
        {
            CodeStatementCollection statements = new CodeStatementCollection();

            using (CodeDomSerializerBase.TraceScope("CollectionCodeDomSerializer::SerializeViaAddRange"))
            {
                if (valuesToSerialize.Count <= 0)
                {
                    return(statements);
                }
                ArrayList         list    = new ArrayList(valuesToSerialize.Count);
                ExpressionContext context = manager.Context[typeof(ExpressionContext)] as ExpressionContext;
                foreach (object obj2 in valuesToSerialize)
                {
                    bool flag = !(obj2 is IComponent);
                    if (!flag)
                    {
                        InheritanceAttribute attribute = (InheritanceAttribute)TypeDescriptor.GetAttributes(obj2)[typeof(InheritanceAttribute)];
                        if (attribute != null)
                        {
                            if (attribute.InheritanceLevel == InheritanceLevel.InheritedReadOnly)
                            {
                                flag = false;
                            }
                            else
                            {
                                flag = true;
                            }
                        }
                        else
                        {
                            flag = true;
                        }
                    }
                    if (flag)
                    {
                        CodeExpression    expression = null;
                        ExpressionContext context2   = null;
                        if (context != null)
                        {
                            context2 = new ExpressionContext(context.Expression, elementType, context.Owner);
                            manager.Context.Push(context2);
                        }
                        try
                        {
                            expression = base.SerializeToExpression(manager, obj2);
                        }
                        finally
                        {
                            if (context2 != null)
                            {
                                manager.Context.Pop();
                            }
                        }
                        if (expression != null)
                        {
                            if ((obj2 != null) && !elementType.IsAssignableFrom(obj2.GetType()))
                            {
                                expression = new CodeCastExpression(elementType, expression);
                            }
                            list.Add(expression);
                        }
                    }
                }
                if (list.Count <= 0)
                {
                    return(statements);
                }
                CodeTypeReference         reference   = new CodeTypeReference(elementType);
                CodeArrayCreateExpression expression2 = new CodeArrayCreateExpression {
                    CreateType = reference
                };
                foreach (CodeExpression expression3 in list)
                {
                    expression2.Initializers.Add(expression3);
                }
                CodeMethodReferenceExpression expression4 = new CodeMethodReferenceExpression(targetExpression, "AddRange");
                CodeMethodInvokeExpression    expression5 = new CodeMethodInvokeExpression {
                    Method = expression4
                };
                expression5.Parameters.Add(expression2);
                statements.Add(new CodeExpressionStatement(expression5));
            }
            return(statements);
        }
コード例 #21
0
        /// <summary>
        ///  This retrieves the value of this property.  If the property returns false
        ///  from ShouldSerializeValue (indicating the ambient value for this property)
        ///  This will look for an AmbientValueAttribute and use it if it can.
        /// </summary>
        private object GetPropertyValue(IDesignerSerializationManager manager, PropertyDescriptor property, object value, out bool validValue)
        {
            object propertyValue = null;

            validValue = true;
            try
            {
                if (!property.ShouldSerializeValue(value))
                {
                    // We aren't supposed to be serializing this property, but we decided to do
                    // it anyway.  Check the property for an AmbientValue attribute and if we
                    // find one, use it's value to serialize.
                    AmbientValueAttribute attr = (AmbientValueAttribute)property.Attributes[typeof(AmbientValueAttribute)];

                    if (attr != null)
                    {
                        return(attr.Value);
                    }
                    else
                    {
                        DefaultValueAttribute defAttr = (DefaultValueAttribute)property.Attributes[typeof(DefaultValueAttribute)];

                        if (defAttr != null)
                        {
                            return(defAttr.Value);
                        }
                        else
                        {
                            // nope, we're not valid...
                            //
                            validValue = false;
                        }
                    }
                }

                propertyValue = property.GetValue(value);
            }
            catch (Exception e)
            {
                // something failed -- we don't have a valid value
                validValue = false;

                manager.ReportError(new CodeDomSerializerException(string.Format(SR.SerializerPropertyGenFailed, property.Name, e.Message), manager));
            }

            if ((propertyValue != null) && (!propertyValue.GetType().IsValueType) && !(propertyValue is Type))
            {
                // DevDiv2 (Dev11) bug 187766 : property whose type implements ISupportInitialize is not
                // serialized with Begin/EndInit.
                Type type = TypeDescriptor.GetProvider(propertyValue).GetReflectionType(typeof(object));
                if (!type.IsDefined(typeof(ProjectTargetFrameworkAttribute), false))
                {
                    // TargetFrameworkProvider is not attached
                    TypeDescriptionProvider typeProvider = CodeDomSerializerBase.GetTargetFrameworkProvider(manager, propertyValue);
                    if (typeProvider != null)
                    {
                        TypeDescriptor.AddProvider(typeProvider, propertyValue);
                    }
                }
            }

            return(propertyValue);
        }
コード例 #22
0
        private CodeArrayCreateExpression SerializeArray(IDesignerSerializationManager manager, Type targetType, ICollection array, ICollection valuesToSerialize)
        {
            CodeArrayCreateExpression expression = null;

            using (CodeDomSerializerBase.TraceScope("CollectionCodeDomSerializer::SerializeArray"))
            {
                if (((Array)array).Rank != 1)
                {
                    manager.ReportError(System.Design.SR.GetString("SerializerInvalidArrayRank", new object[] { ((Array)array).Rank.ToString(CultureInfo.InvariantCulture) }));
                    return(expression);
                }
                Type elementType = targetType.GetElementType();
                CodeTypeReference         reference   = new CodeTypeReference(elementType);
                CodeArrayCreateExpression expression2 = new CodeArrayCreateExpression {
                    CreateType = reference
                };
                bool flag = true;
                foreach (object obj2 in valuesToSerialize)
                {
                    if ((obj2 is IComponent) && TypeDescriptor.GetAttributes(obj2).Contains(InheritanceAttribute.InheritedReadOnly))
                    {
                        flag = false;
                        break;
                    }
                    CodeExpression    expression3 = null;
                    ExpressionContext context     = null;
                    ExpressionContext context2    = manager.Context[typeof(ExpressionContext)] as ExpressionContext;
                    if (context2 != null)
                    {
                        context = new ExpressionContext(context2.Expression, elementType, context2.Owner);
                        manager.Context.Push(context);
                    }
                    try
                    {
                        expression3 = base.SerializeToExpression(manager, obj2);
                    }
                    finally
                    {
                        if (context != null)
                        {
                            manager.Context.Pop();
                        }
                    }
                    if (expression3 != null)
                    {
                        if ((obj2 != null) && (obj2.GetType() != elementType))
                        {
                            expression3 = new CodeCastExpression(elementType, expression3);
                        }
                        expression2.Initializers.Add(expression3);
                    }
                    else
                    {
                        flag = false;
                        break;
                    }
                }
                if (flag)
                {
                    expression = expression2;
                }
            }
            return(expression);
        }
コード例 #23
0
 private object Serialize(IDesignerSerializationManager manager, object value, bool forceInvariant, bool shouldSerializeInvariant, bool ensureInvariant)
 {
     using (CodeDomSerializerBase.TraceScope("ResourceCodeDomSerializer::Serialize"))
     {
         bool   flag;
         string str3;
         SerializationResourceManager resourceManager = this.GetResourceManager(manager);
         CodeStatementCollection      statements      = (CodeStatementCollection)manager.Context[typeof(CodeStatementCollection)];
         if (!forceInvariant)
         {
             if (!resourceManager.DeclarationAdded)
             {
                 resourceManager.DeclarationAdded = true;
                 RootContext context = manager.Context[typeof(RootContext)] as RootContext;
                 if (statements != null)
                 {
                     CodeExpression[] expressionArray;
                     if (context != null)
                     {
                         string name = manager.GetName(context.Value);
                         expressionArray = new CodeExpression[] { new CodeTypeOfExpression(name) };
                     }
                     else
                     {
                         expressionArray = new CodeExpression[] { new CodePrimitiveExpression(this.ResourceManagerName) };
                     }
                     CodeExpression initExpression = new CodeObjectCreateExpression(typeof(ComponentResourceManager), expressionArray);
                     statements.Add(new CodeVariableDeclarationStatement(typeof(ComponentResourceManager), this.ResourceManagerName, initExpression));
                     base.SetExpression(manager, resourceManager, new CodeVariableReferenceExpression(this.ResourceManagerName));
                     resourceManager.ExpressionAdded = true;
                 }
             }
             else if (!resourceManager.ExpressionAdded)
             {
                 if (base.GetExpression(manager, resourceManager) == null)
                 {
                     base.SetExpression(manager, resourceManager, new CodeVariableReferenceExpression(this.ResourceManagerName));
                 }
                 resourceManager.ExpressionAdded = true;
             }
         }
         ExpressionContext tree = (ExpressionContext)manager.Context[typeof(ExpressionContext)];
         string            str2 = resourceManager.SetValue(manager, tree, value, forceInvariant, shouldSerializeInvariant, ensureInvariant, false);
         if ((value is string) || ((tree != null) && (tree.ExpressionType == typeof(string))))
         {
             flag = false;
             str3 = "GetString";
         }
         else
         {
             flag = true;
             str3 = "GetObject";
         }
         CodeMethodInvokeExpression expression = new CodeMethodInvokeExpression {
             Method = new CodeMethodReferenceExpression(new CodeVariableReferenceExpression(this.ResourceManagerName), str3)
         };
         expression.Parameters.Add(new CodePrimitiveExpression(str2));
         if (flag)
         {
             System.Type castType = this.GetCastType(manager, value);
             if (castType != null)
             {
                 return(new CodeCastExpression(castType, expression));
             }
             return(expression);
         }
         return(expression);
     }
 }
コード例 #24
0
        public virtual object Deserialize(IDesignerSerializationManager manager, CodeTypeDeclaration declaration)
        {
            if (manager == null)
            {
                throw new ArgumentNullException("manager");
            }
            if (declaration == null)
            {
                throw new ArgumentNullException("declaration");
            }
            object obj2 = null;

            using (CodeDomSerializerBase.TraceScope("TypeCodeDomSerializer::Deserialize"))
            {
                bool            caseInsensitive = false;
                CodeDomProvider service         = manager.GetService(typeof(CodeDomProvider)) as CodeDomProvider;
                if (service != null)
                {
                    caseInsensitive = (service.LanguageOptions & LanguageOptions.CaseInsensitive) != LanguageOptions.None;
                }
                Type   type = null;
                string name = declaration.Name;
                foreach (CodeTypeReference reference in declaration.BaseTypes)
                {
                    Type type2 = manager.GetType(CodeDomSerializerBase.GetTypeNameFromCodeTypeReference(manager, reference));
                    name = reference.BaseType;
                    if ((type2 != null) && !type2.IsInterface)
                    {
                        type = type2;
                        break;
                    }
                }
                if (type == null)
                {
                    CodeDomSerializerBase.Error(manager, System.Design.SR.GetString("SerializerTypeNotFound", new object[] { name }), "SerializerTypeNotFound");
                }
                if (CodeDomSerializerBase.GetReflectionTypeFromTypeHelper(manager, type).IsAbstract)
                {
                    CodeDomSerializerBase.Error(manager, System.Design.SR.GetString("SerializerTypeAbstract", new object[] { type.FullName }), "SerializerTypeAbstract");
                }
                ResolveNameEventHandler handler = new ResolveNameEventHandler(this.OnResolveName);
                manager.ResolveName += handler;
                obj2 = manager.CreateInstance(type, null, declaration.Name, true);
                int count = declaration.Members.Count;
                this._nameTable      = new HybridDictionary(count, caseInsensitive);
                this._statementTable = new Dictionary <string, CodeDomSerializerBase.OrderedCodeStatementCollection>(count);
                Dictionary <string, string> names = new Dictionary <string, string>(count);
                RootContext context = new RootContext(new CodeThisReferenceExpression(), obj2);
                manager.Context.Push(context);
                try
                {
                    StringComparison comparisonType = caseInsensitive ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
                    foreach (CodeTypeMember member in declaration.Members)
                    {
                        CodeMemberField field = member as CodeMemberField;
                        if ((field != null) && !string.Equals(field.Name, declaration.Name, comparisonType))
                        {
                            this._nameTable[field.Name] = field;
                            if ((field.Type != null) && !string.IsNullOrEmpty(field.Type.BaseType))
                            {
                                names[field.Name] = CodeDomSerializerBase.GetTypeNameFromCodeTypeReference(manager, field.Type);
                            }
                        }
                    }
                    CodeMemberMethod[] initializeMethods = this.GetInitializeMethods(manager, declaration);
                    if (initializeMethods == null)
                    {
                        throw new InvalidOperationException();
                    }
                    foreach (CodeMemberMethod method in initializeMethods)
                    {
                        foreach (CodeStatement statement in method.Statements)
                        {
                            CodeVariableDeclarationStatement statement2 = statement as CodeVariableDeclarationStatement;
                            if (statement2 != null)
                            {
                                this._nameTable[statement2.Name] = statement;
                            }
                        }
                    }
                    this._nameTable[declaration.Name] = context.Expression;
                    foreach (CodeMemberMethod method2 in initializeMethods)
                    {
                        CodeDomSerializerBase.FillStatementTable(manager, this._statementTable, names, method2.Statements, declaration.Name);
                    }
                    PropertyDescriptor descriptor = manager.Properties["SupportsStatementGeneration"];
                    if (((descriptor != null) && (descriptor.PropertyType == typeof(bool))) && ((bool)descriptor.GetValue(manager)))
                    {
                        foreach (string str2 in this._nameTable.Keys)
                        {
                            if (!this._statementTable.ContainsKey(str2))
                            {
                                continue;
                            }
                            CodeStatementCollection statements = this._statementTable[str2];
                            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(str2);
                            }
                        }
                    }
                    base.DeserializePropertiesFromResources(manager, obj2, _designTimeFilter);
                    CodeDomSerializerBase.OrderedCodeStatementCollection[] array = new CodeDomSerializerBase.OrderedCodeStatementCollection[this._statementTable.Count];
                    this._statementTable.Values.CopyTo(array, 0);
                    Array.Sort(array, StatementOrderComparer.Default);
                    CodeDomSerializerBase.OrderedCodeStatementCollection statements2 = null;
                    foreach (CodeDomSerializerBase.OrderedCodeStatementCollection statements3 in array)
                    {
                        if (statements3.Name.Equals(declaration.Name))
                        {
                            statements2 = statements3;
                        }
                        else
                        {
                            this.DeserializeName(manager, statements3.Name, statements3);
                        }
                    }
                    if (statements2 != null)
                    {
                        this.DeserializeName(manager, statements2.Name, statements2);
                    }
                }
                finally
                {
                    this._nameTable      = null;
                    this._statementTable = null;
                    manager.ResolveName -= handler;
                    manager.Context.Pop();
                }
            }
            return(obj2);
        }
コード例 #25
0
        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);
        }
コード例 #26
0
        public override object Serialize(IDesignerSerializationManager manager, object value)
        {
            CodeStatementCollection      statements = null;
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);

            using (CodeDomSerializerBase.TraceScope("ComponentCodeDomSerializer::Serialize"))
            {
                if ((manager == null) || (value == null))
                {
                    throw new ArgumentNullException((manager == null) ? "manager" : "value");
                }
                if (base.IsSerialized(manager, value))
                {
                    return(base.GetExpression(manager, value));
                }
                InheritanceLevel     notInherited = InheritanceLevel.NotInherited;
                InheritanceAttribute attribute    = (InheritanceAttribute)TypeDescriptor.GetAttributes(value)[typeof(InheritanceAttribute)];
                if (attribute != null)
                {
                    notInherited = attribute.InheritanceLevel;
                }
                if (notInherited == InheritanceLevel.InheritedReadOnly)
                {
                    return(statements);
                }
                statements = new CodeStatementCollection();
                CodeTypeDeclaration declaration = manager.Context[typeof(CodeTypeDeclaration)] as CodeTypeDeclaration;
                RootContext         context     = manager.Context[typeof(RootContext)] as RootContext;
                CodeExpression      left        = null;
                bool flag  = false;
                bool flag2 = true;
                bool flag3 = true;
                bool flag4 = false;
                left = base.GetExpression(manager, value);
                if (left != null)
                {
                    flag  = false;
                    flag2 = false;
                    flag3 = false;
                    IComponent component = value as IComponent;
                    if ((component != null) && (component.Site == null))
                    {
                        ExpressionContext context2 = manager.Context[typeof(ExpressionContext)] as ExpressionContext;
                        if ((context2 == null) || (context2.PresetValue != value))
                        {
                            flag4 = true;
                        }
                    }
                }
                else
                {
                    if (notInherited == InheritanceLevel.NotInherited)
                    {
                        PropertyDescriptor descriptor = properties["GenerateMember"];
                        if (((descriptor != null) && (descriptor.PropertyType == typeof(bool))) && !((bool)descriptor.GetValue(value)))
                        {
                            flag  = true;
                            flag2 = false;
                        }
                    }
                    else
                    {
                        flag3 = false;
                    }
                    if (context == null)
                    {
                        flag  = true;
                        flag2 = false;
                    }
                }
                manager.Context.Push(value);
                manager.Context.Push(statements);
                try
                {
                    try
                    {
                        string name      = manager.GetName(value);
                        string className = TypeDescriptor.GetClassName(value);
                        if ((flag2 || flag) && (name != null))
                        {
                            if (flag2)
                            {
                                if (notInherited == InheritanceLevel.NotInherited)
                                {
                                    MemberAttributes   @private;
                                    CodeMemberField    field       = new CodeMemberField(className, name);
                                    PropertyDescriptor descriptor2 = properties["Modifiers"];
                                    if (descriptor2 == null)
                                    {
                                        descriptor2 = properties["DefaultModifiers"];
                                    }
                                    if ((descriptor2 != null) && (descriptor2.PropertyType == typeof(MemberAttributes)))
                                    {
                                        @private = (MemberAttributes)descriptor2.GetValue(value);
                                    }
                                    else
                                    {
                                        @private = MemberAttributes.Private;
                                    }
                                    field.Attributes = @private;
                                    declaration.Members.Add(field);
                                }
                                left = new CodeFieldReferenceExpression(context.Expression, name);
                            }
                            else
                            {
                                if (notInherited == InheritanceLevel.NotInherited)
                                {
                                    CodeVariableDeclarationStatement statement = new CodeVariableDeclarationStatement(className, name);
                                    statements.Add(statement);
                                }
                                left = new CodeVariableReferenceExpression(name);
                            }
                        }
                        if (flag3)
                        {
                            CodeExpression  expression2;
                            IContainer      service = manager.GetService(typeof(IContainer)) as IContainer;
                            ConstructorInfo info    = null;
                            if (service != null)
                            {
                                info = CodeDomSerializerBase.GetReflectionTypeHelper(manager, value).GetConstructor(BindingFlags.ExactBinding | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly, null, this.GetContainerConstructor(manager), null);
                            }
                            if (info != null)
                            {
                                expression2 = new CodeObjectCreateExpression(className, new CodeExpression[] { base.SerializeToExpression(manager, service) });
                            }
                            else
                            {
                                bool flag5;
                                expression2 = base.SerializeCreationExpression(manager, value, out flag5);
                            }
                            if (expression2 != null)
                            {
                                if (left == null)
                                {
                                    if (flag4)
                                    {
                                        left = expression2;
                                    }
                                }
                                else
                                {
                                    CodeAssignStatement statement2 = new CodeAssignStatement(left, expression2);
                                    statements.Add(statement2);
                                }
                            }
                        }
                        if (left != null)
                        {
                            base.SetExpression(manager, value, left);
                        }
                        if ((left != null) && !flag4)
                        {
                            bool flag6 = value is ISupportInitialize;
                            if (flag6)
                            {
                                string fullName = typeof(ISupportInitialize).FullName;
                                flag6 = manager.GetType(fullName) != null;
                            }
                            Type c = null;
                            if (flag6)
                            {
                                c     = CodeDomSerializerBase.GetReflectionTypeHelper(manager, value);
                                flag6 = CodeDomSerializerBase.GetReflectionTypeFromTypeHelper(manager, typeof(ISupportInitialize)).IsAssignableFrom(c);
                            }
                            bool flag7 = (value is IPersistComponentSettings) && ((IPersistComponentSettings)value).SaveSettings;
                            if (flag7)
                            {
                                string typeName = typeof(IPersistComponentSettings).FullName;
                                flag7 = manager.GetType(typeName) != null;
                            }
                            if (flag7)
                            {
                                c     = c ?? CodeDomSerializerBase.GetReflectionTypeHelper(manager, value);
                                flag7 = CodeDomSerializerBase.GetReflectionTypeFromTypeHelper(manager, typeof(IPersistComponentSettings)).IsAssignableFrom(c);
                            }
                            IDesignerSerializationManager manager2 = (IDesignerSerializationManager)manager.GetService(typeof(IDesignerSerializationManager));
                            if (flag6)
                            {
                                this.SerializeSupportInitialize(manager, statements, left, value, "BeginInit");
                            }
                            base.SerializePropertiesToResources(manager, statements, value, _designTimeFilter);
                            ComponentCache       serviceInstance = (ComponentCache)manager.GetService(typeof(ComponentCache));
                            ComponentCache.Entry entry           = null;
                            if (serviceInstance == null)
                            {
                                IServiceContainer container2 = (IServiceContainer)manager.GetService(typeof(IServiceContainer));
                                if (container2 != null)
                                {
                                    serviceInstance = new ComponentCache(manager);
                                    container2.AddService(typeof(ComponentCache), serviceInstance);
                                }
                            }
                            else if (((manager == manager2) && (serviceInstance != null)) && serviceInstance.Enabled)
                            {
                                entry = serviceInstance[value];
                            }
                            if ((entry == null) || entry.Tracking)
                            {
                                if (entry == null)
                                {
                                    entry = new ComponentCache.Entry(serviceInstance);
                                    ComponentCache.Entry entryAll = null;
                                    entryAll = serviceInstance.GetEntryAll(value);
                                    if (((entryAll != null) && (entryAll.Dependencies != null)) && (entryAll.Dependencies.Count > 0))
                                    {
                                        foreach (object obj2 in entryAll.Dependencies)
                                        {
                                            entry.AddDependency(obj2);
                                        }
                                    }
                                }
                                entry.Component = value;
                                bool flag8 = manager == manager2;
                                entry.Valid = flag8 && this.CanCacheComponent(manager, value, properties);
                                if ((flag8 && (serviceInstance != null)) && serviceInstance.Enabled)
                                {
                                    manager.Context.Push(serviceInstance);
                                    manager.Context.Push(entry);
                                }
                                try
                                {
                                    entry.Statements = new CodeStatementCollection();
                                    base.SerializeProperties(manager, entry.Statements, value, _runTimeFilter);
                                    base.SerializeEvents(manager, entry.Statements, value, null);
                                    foreach (CodeStatement statement3 in entry.Statements)
                                    {
                                        if (statement3 is CodeVariableDeclarationStatement)
                                        {
                                            entry.Tracking = true;
                                            break;
                                        }
                                    }
                                    if (entry.Statements.Count > 0)
                                    {
                                        entry.Statements.Insert(0, new CodeCommentStatement(string.Empty));
                                        entry.Statements.Insert(0, new CodeCommentStatement(name));
                                        entry.Statements.Insert(0, new CodeCommentStatement(string.Empty));
                                        if ((flag8 && (serviceInstance != null)) && serviceInstance.Enabled)
                                        {
                                            serviceInstance[value] = entry;
                                        }
                                    }
                                }
                                finally
                                {
                                    if ((flag8 && (serviceInstance != null)) && serviceInstance.Enabled)
                                    {
                                        manager.Context.Pop();
                                        manager.Context.Pop();
                                    }
                                }
                            }
                            else if (((entry.Resources != null) || (entry.Metadata != null)) && ((serviceInstance != null) && serviceInstance.Enabled))
                            {
                                ResourceCodeDomSerializer.Default.ApplyCacheEntry(manager, entry);
                            }
                            statements.AddRange(entry.Statements);
                            if (flag7)
                            {
                                this.SerializeLoadComponentSettings(manager, statements, left, value);
                            }
                            if (flag6)
                            {
                                this.SerializeSupportInitialize(manager, statements, left, value, "EndInit");
                            }
                        }
                        return(statements);
                    }
                    catch (CheckoutException)
                    {
                        throw;
                    }
                    catch (Exception exception)
                    {
                        manager.ReportError(exception);
                    }
                    return(statements);
                }
                finally
                {
                    manager.Context.Pop();
                    manager.Context.Pop();
                }
            }
            return(statements);
        }
コード例 #27
0
        public virtual object Serialize(IDesignerSerializationManager manager, object value)
        {
            object obj2 = null;

            if ((manager == null) || (value == null))
            {
                throw new ArgumentNullException((manager == null) ? "manager" : "value");
            }
            using (CodeDomSerializerBase.TraceScope("CodeDomSerializer::Serialize"))
            {
                bool flag2;
                bool flag3;
                if (value is Type)
                {
                    return(new CodeTypeOfExpression((Type)value));
                }
                bool           flag       = false;
                CodeExpression expression = base.SerializeCreationExpression(manager, value, out flag2);
                if (!(value is IComponent))
                {
                    flag = flag2;
                }
                ExpressionContext context = manager.Context[typeof(ExpressionContext)] as ExpressionContext;
                if ((context != null) && object.ReferenceEquals(context.PresetValue, value))
                {
                    flag3 = true;
                }
                else
                {
                    flag3 = false;
                }
                if (expression == null)
                {
                    return(obj2);
                }
                if (flag)
                {
                    return(expression);
                }
                CodeStatementCollection statements = new CodeStatementCollection();
                if (flag3)
                {
                    base.SetExpression(manager, value, expression, true);
                }
                else
                {
                    string uniqueName = base.GetUniqueName(manager, value);
                    CodeVariableDeclarationStatement statement = new CodeVariableDeclarationStatement(TypeDescriptor.GetClassName(value), uniqueName)
                    {
                        InitExpression = expression
                    };
                    statements.Add(statement);
                    CodeExpression expression2 = new CodeVariableReferenceExpression(uniqueName);
                    base.SetExpression(manager, value, expression2);
                }
                base.SerializePropertiesToResources(manager, statements, value, _designTimeFilter);
                base.SerializeProperties(manager, statements, value, _runTimeFilter);
                base.SerializeEvents(manager, statements, value, _runTimeFilter);
                return(statements);
            }
        }