private void ExportProperty(CodeTypeDeclaration codeClass, MemberMapping member, string ns, CodeIdentifiers memberScope, CodeConstructor ctor)
        {
            string          name     = memberScope.AddUnique(CodeExporter.MakeFieldName(member.Name), member);
            string          typeName = member.GetTypeName(base.CodeProvider);
            CodeMemberField field    = new CodeMemberField(typeName, name)
            {
                Attributes = MemberAttributes.Private
            };

            codeClass.Members.Add(field);
            CodeMemberProperty property = base.CreatePropertyDeclaration(field, member.Name, typeName);

            property.Comments.Add(new CodeCommentStatement(Res.GetString("XmlRemarks"), true));
            this.AddMemberMetadata(field, property.CustomAttributes, member, ns, false, property.Comments, ctor);
            codeClass.Members.Add(property);
            if (member.CheckSpecified != SpecifiedAccessor.None)
            {
                field = new CodeMemberField(typeof(bool).FullName, name + "Specified")
                {
                    Attributes = MemberAttributes.Private
                };
                codeClass.Members.Add(field);
                property = base.CreatePropertyDeclaration(field, member.Name + "Specified", typeof(bool).FullName);
                property.Comments.Add(new CodeCommentStatement(Res.GetString("XmlRemarks"), true));
                CodeAttributeDeclaration declaration = new CodeAttributeDeclaration(typeof(XmlIgnoreAttribute).FullName);
                property.CustomAttributes.Add(declaration);
                codeClass.Members.Add(property);
            }
        }
        void ExportProperty(CodeTypeDeclaration codeClass, MemberMapping member, CodeIdentifiers memberScope)
        {
            string fieldName = memberScope.AddUnique(CodeExporter.MakeFieldName(member.Name), member);
            string fieldType = member.GetTypeName(CodeProvider);
            // need to create a private field
            CodeMemberField field = new CodeMemberField(fieldType, fieldName);

            field.Attributes = MemberAttributes.Private;
            codeClass.Members.Add(field);

            CodeMemberProperty prop = CreatePropertyDeclaration(field, member.Name, fieldType);

            prop.Comments.Add(new CodeCommentStatement(Res.GetString(Res.XmlRemarks), true));
            AddMemberMetadata(prop.CustomAttributes, member, false);
            codeClass.Members.Add(prop);

            if (member.CheckSpecified != SpecifiedAccessor.None)
            {
                field            = new CodeMemberField(typeof(bool).FullName, fieldName + "Specified");
                field.Attributes = MemberAttributes.Private;
                codeClass.Members.Add(field);

                prop = CreatePropertyDeclaration(field, member.Name + "Specified", typeof(bool).FullName);
                prop.Comments.Add(new CodeCommentStatement(Res.GetString(Res.XmlRemarks), true));
                CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(typeof(SoapIgnoreAttribute).FullName);
                prop.CustomAttributes.Add(attribute);
                codeClass.Members.Add(prop);
            }
        }
 private static void DropDefaultAttribute(Accessor accessor, CodeCommentStatementCollection comments, string type)
 {
     if (!accessor.IsFixed && accessor.IsOptional)
     {
         CodeExporter.AddWarningComment(comments, Res.GetString("XmlDropDefaultAttribute", new object[] { type }));
     }
 }
Beispiel #4
0
        private CodeTypeDeclaration ExportStruct(StructMapping mapping)
        {
            if (mapping.TypeDesc.IsRoot)
            {
                base.ExportRoot(mapping, typeof(SoapIncludeAttribute));
                return(null);
            }
            if (!mapping.IncludeInSchema)
            {
                return(null);
            }
            string name = mapping.TypeDesc.Name;
            string str2 = (mapping.TypeDesc.BaseTypeDesc == null) ? string.Empty : mapping.TypeDesc.BaseTypeDesc.Name;
            CodeTypeDeclaration declaration = new CodeTypeDeclaration(name)
            {
                IsPartial = base.CodeProvider.Supports(GeneratorSupport.PartialTypes)
            };

            declaration.Comments.Add(new CodeCommentStatement(Res.GetString("XmlRemarks"), true));
            base.CodeNamespace.Types.Add(declaration);
            if ((str2 != null) && (str2.Length > 0))
            {
                declaration.BaseTypes.Add(str2);
            }
            else
            {
                base.AddPropertyChangedNotifier(declaration);
            }
            declaration.TypeAttributes |= TypeAttributes.Public;
            if (mapping.TypeDesc.IsAbstract)
            {
                declaration.TypeAttributes |= TypeAttributes.Abstract;
            }
            CodeExporter.AddIncludeMetadata(declaration.CustomAttributes, mapping, typeof(SoapIncludeAttribute));
            if (base.GenerateProperties)
            {
                for (int j = 0; j < mapping.Members.Length; j++)
                {
                    this.ExportProperty(declaration, mapping.Members[j], mapping.Scope);
                }
            }
            else
            {
                for (int k = 0; k < mapping.Members.Length; k++)
                {
                    this.ExportMember(declaration, mapping.Members[k]);
                }
            }
            for (int i = 0; i < mapping.Members.Length; i++)
            {
                this.EnsureTypesExported(mapping.Members[i].Elements, null);
            }
            if (mapping.BaseMapping != null)
            {
                this.ExportType(mapping.BaseMapping);
            }
            this.ExportDerivedStructs(mapping);
            CodeGenerator.ValidateIdentifiers(declaration);
            return(declaration);
        }
 private void ExportType(TypeMapping mapping, string name, string ns, ElementAccessor rootElement, bool checkReference)
 {
     if ((!mapping.IsReference || (mapping.Namespace == "http://schemas.xmlsoap.org/soap/encoding/")) && ((!(mapping is StructMapping) || !checkReference) || (!((StructMapping)mapping).ReferencedByTopLevelElement || (rootElement != null))))
     {
         if (((mapping is ArrayMapping) && (rootElement != null)) && (rootElement.IsTopLevelInSchema && (((ArrayMapping)mapping).TopLevelMapping != null)))
         {
             mapping = ((ArrayMapping)mapping).TopLevelMapping;
         }
         CodeTypeDeclaration declaration = null;
         if (base.ExportedMappings[mapping] == null)
         {
             base.ExportedMappings.Add(mapping, mapping);
             if (mapping.TypeDesc.IsMappedType)
             {
                 declaration = mapping.TypeDesc.ExtendedType.ExportTypeDefinition(base.CodeNamespace, base.CodeCompileUnit);
             }
             else if (mapping is EnumMapping)
             {
                 declaration = base.ExportEnum((EnumMapping)mapping, typeof(XmlEnumAttribute));
             }
             else if (mapping is StructMapping)
             {
                 declaration = this.ExportStruct((StructMapping)mapping);
             }
             else if (mapping is ArrayMapping)
             {
                 this.EnsureTypesExported(((ArrayMapping)mapping).Elements, ns);
             }
             if (declaration != null)
             {
                 if (!mapping.TypeDesc.IsMappedType)
                 {
                     declaration.CustomAttributes.Add(base.GeneratedCodeAttribute);
                     declaration.CustomAttributes.Add(new CodeAttributeDeclaration(typeof(SerializableAttribute).FullName));
                     if (!declaration.IsEnum)
                     {
                         declaration.CustomAttributes.Add(new CodeAttributeDeclaration(typeof(DebuggerStepThroughAttribute).FullName));
                         declaration.CustomAttributes.Add(new CodeAttributeDeclaration(typeof(DesignerCategoryAttribute).FullName, new CodeAttributeArgument[] { new CodeAttributeArgument(new CodePrimitiveExpression("code")) }));
                     }
                     base.AddTypeMetadata(declaration.CustomAttributes, typeof(XmlTypeAttribute), mapping.TypeDesc.Name, Accessor.UnescapeName(mapping.TypeName), mapping.Namespace, mapping.IncludeInSchema);
                 }
                 else if (CodeExporter.FindAttributeDeclaration(typeof(GeneratedCodeAttribute), declaration.CustomAttributes) == null)
                 {
                     declaration.CustomAttributes.Add(base.GeneratedCodeAttribute);
                 }
                 base.ExportedClasses.Add(mapping, declaration);
             }
         }
         else
         {
             declaration = (CodeTypeDeclaration)base.ExportedClasses[mapping];
         }
         if ((declaration != null) && (rootElement != null))
         {
             this.AddRootMetadata(declaration.CustomAttributes, mapping, name, ns, rootElement);
         }
     }
 }
        CodeTypeDeclaration ExportStruct(StructMapping mapping)
        {
            if (mapping.TypeDesc.IsRoot)
            {
                ExportRoot(mapping, typeof(SoapIncludeAttribute));
                return(null);
            }
            if (!mapping.IncludeInSchema)
            {
                return(null);
            }

            string className = mapping.TypeDesc.Name;
            string baseName  = mapping.TypeDesc.BaseTypeDesc == null ? string.Empty : mapping.TypeDesc.BaseTypeDesc.Name;

            CodeTypeDeclaration codeClass = new CodeTypeDeclaration(className);

            codeClass.IsPartial = CodeProvider.Supports(GeneratorSupport.PartialTypes);
            codeClass.Comments.Add(new CodeCommentStatement(Res.GetString(Res.XmlRemarks), true));

            CodeNamespace.Types.Add(codeClass);

            if (baseName != null && baseName.Length > 0)
            {
                codeClass.BaseTypes.Add(baseName);
            }
            else
            {
                AddPropertyChangedNotifier(codeClass);
            }

            codeClass.TypeAttributes |= TypeAttributes.Public;
            if (mapping.TypeDesc.IsAbstract)
            {
                codeClass.TypeAttributes |= TypeAttributes.Abstract;
            }

            CodeExporter.AddIncludeMetadata(codeClass.CustomAttributes, mapping, typeof(SoapIncludeAttribute));

            if (GenerateProperties)
            {
                for (int i = 0; i < mapping.Members.Length; i++)
                {
                    ExportProperty(codeClass, mapping.Members[i], mapping.Scope);
                }
            }
            else
            {
                for (int i = 0; i < mapping.Members.Length; i++)
                {
                    ExportMember(codeClass, mapping.Members[i]);
                }
            }
            for (int i = 0; i < mapping.Members.Length; i++)
            {
                EnsureTypesExported(mapping.Members[i].Elements, null);
            }

            if (mapping.BaseMapping != null)
            {
                ExportType(mapping.BaseMapping);
            }

            ExportDerivedStructs(mapping);
            CodeGenerator.ValidateIdentifiers(codeClass);
            return(codeClass);
        }
        private CodeAttributeArgument[] GetDefaultValueArguments(PrimitiveMapping mapping, object value, out CodeExpression initExpression)
        {
            initExpression = null;
            if (value == null)
            {
                return(null);
            }
            CodeExpression left        = null;
            CodeExpression expression2 = null;
            Type           type        = value.GetType();

            CodeAttributeArgument[] argumentArray = null;
            if (mapping is EnumMapping)
            {
                if (((EnumMapping)mapping).IsFlags)
                {
                    string[] strArray = ((string)value).Split(null);
                    for (int i = 0; i < strArray.Length; i++)
                    {
                        if (strArray[i].Length != 0)
                        {
                            CodeExpression right = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(mapping.TypeDesc.FullName), strArray[i]);
                            if (left != null)
                            {
                                left = new CodeBinaryOperatorExpression(left, CodeBinaryOperatorType.BitwiseOr, right);
                            }
                            else
                            {
                                left = right;
                            }
                        }
                    }
                }
                else
                {
                    left = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(mapping.TypeDesc.FullName), (string)value);
                }
                initExpression = left;
                argumentArray  = new CodeAttributeArgument[] { new CodeAttributeArgument(left) };
            }
            else if (((type == typeof(bool)) || (type == typeof(int))) || ((type == typeof(string)) || (type == typeof(double))))
            {
                initExpression = left = new CodePrimitiveExpression(value);
                argumentArray  = new CodeAttributeArgument[] { new CodeAttributeArgument(left) };
            }
            else if (((type == typeof(short)) || (type == typeof(long))) || (((type == typeof(float)) || (type == typeof(byte))) || (type == typeof(decimal))))
            {
                left           = new CodePrimitiveExpression(Convert.ToString(value, NumberFormatInfo.InvariantInfo));
                expression2    = new CodeTypeOfExpression(type.FullName);
                argumentArray  = new CodeAttributeArgument[] { new CodeAttributeArgument(expression2), new CodeAttributeArgument(left) };
                initExpression = new CodeCastExpression(type.FullName, new CodePrimitiveExpression(value));
            }
            else if (((type == typeof(sbyte)) || (type == typeof(ushort))) || ((type == typeof(uint)) || (type == typeof(ulong))))
            {
                value          = CodeExporter.PromoteType(type, value);
                left           = new CodePrimitiveExpression(Convert.ToString(value, NumberFormatInfo.InvariantInfo));
                expression2    = new CodeTypeOfExpression(type.FullName);
                argumentArray  = new CodeAttributeArgument[] { new CodeAttributeArgument(expression2), new CodeAttributeArgument(left) };
                initExpression = new CodeCastExpression(type.FullName, new CodePrimitiveExpression(value));
            }
            else if (type == typeof(DateTime))
            {
                string   str;
                long     ticks;
                DateTime time = (DateTime)value;
                if (mapping.TypeDesc.FormatterName == "Date")
                {
                    str = XmlCustomFormatter.FromDate(time);
                    DateTime time2 = new DateTime(time.Year, time.Month, time.Day);
                    ticks = time2.Ticks;
                }
                else if (mapping.TypeDesc.FormatterName == "Time")
                {
                    str   = XmlCustomFormatter.FromDateTime(time);
                    ticks = time.Ticks;
                }
                else
                {
                    str   = XmlCustomFormatter.FromDateTime(time);
                    ticks = time.Ticks;
                }
                left           = new CodePrimitiveExpression(str);
                expression2    = new CodeTypeOfExpression(type.FullName);
                argumentArray  = new CodeAttributeArgument[] { new CodeAttributeArgument(expression2), new CodeAttributeArgument(left) };
                initExpression = new CodeObjectCreateExpression(new CodeTypeReference(typeof(DateTime)), new CodeExpression[] { new CodePrimitiveExpression(ticks) });
            }
            else if (type == typeof(Guid))
            {
                left           = new CodePrimitiveExpression(Convert.ToString(value, NumberFormatInfo.InvariantInfo));
                expression2    = new CodeTypeOfExpression(type.FullName);
                argumentArray  = new CodeAttributeArgument[] { new CodeAttributeArgument(expression2), new CodeAttributeArgument(left) };
                initExpression = new CodeObjectCreateExpression(new CodeTypeReference(typeof(Guid)), new CodeExpression[] { left });
            }
            if ((mapping.TypeDesc.FullName != type.ToString()) && !(mapping is EnumMapping))
            {
                initExpression = new CodeCastExpression(mapping.TypeDesc.FullName, initExpression);
            }
            return(argumentArray);
        }
        private CodeTypeDeclaration ExportStruct(StructMapping mapping)
        {
            CodeConstructor constructor;

            if (mapping.TypeDesc.IsRoot)
            {
                base.ExportRoot(mapping, typeof(XmlIncludeAttribute));
                return(null);
            }
            string name = mapping.TypeDesc.Name;
            string str2 = ((mapping.TypeDesc.BaseTypeDesc == null) || mapping.TypeDesc.BaseTypeDesc.IsRoot) ? string.Empty : mapping.TypeDesc.BaseTypeDesc.FullName;
            CodeTypeDeclaration declaration = new CodeTypeDeclaration(name)
            {
                IsPartial = base.CodeProvider.Supports(GeneratorSupport.PartialTypes)
            };

            declaration.Comments.Add(new CodeCommentStatement(Res.GetString("XmlRemarks"), true));
            base.CodeNamespace.Types.Add(declaration);
            constructor = new CodeConstructor {
                Attributes = (constructor.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public
            };
            declaration.Members.Add(constructor);
            if (mapping.TypeDesc.IsAbstract)
            {
                constructor.Attributes |= MemberAttributes.Abstract;
            }
            if ((str2 != null) && (str2.Length > 0))
            {
                declaration.BaseTypes.Add(str2);
            }
            else
            {
                base.AddPropertyChangedNotifier(declaration);
            }
            declaration.TypeAttributes |= TypeAttributes.Public;
            if (mapping.TypeDesc.IsAbstract)
            {
                declaration.TypeAttributes |= TypeAttributes.Abstract;
            }
            CodeExporter.AddIncludeMetadata(declaration.CustomAttributes, mapping, typeof(XmlIncludeAttribute));
            if (mapping.IsSequence)
            {
                int num = 0;
                for (int j = 0; j < mapping.Members.Length; j++)
                {
                    MemberMapping mapping2 = mapping.Members[j];
                    if (mapping2.IsParticle && (mapping2.SequenceId < 0))
                    {
                        mapping2.SequenceId = num++;
                    }
                }
            }
            if (base.GenerateProperties)
            {
                for (int k = 0; k < mapping.Members.Length; k++)
                {
                    this.ExportProperty(declaration, mapping.Members[k], mapping.Namespace, mapping.Scope, constructor);
                }
            }
            else
            {
                for (int m = 0; m < mapping.Members.Length; m++)
                {
                    this.ExportMember(declaration, mapping.Members[m], mapping.Namespace, constructor);
                }
            }
            for (int i = 0; i < mapping.Members.Length; i++)
            {
                if (mapping.Members[i].Xmlns == null)
                {
                    this.EnsureTypesExported(mapping.Members[i].Elements, mapping.Namespace);
                    this.EnsureTypesExported(mapping.Members[i].Attribute, mapping.Namespace);
                    this.EnsureTypesExported(mapping.Members[i].Text, mapping.Namespace);
                }
            }
            if (mapping.BaseMapping != null)
            {
                this.ExportType(mapping.BaseMapping, null, mapping.Namespace, null, false);
            }
            this.ExportDerivedStructs(mapping);
            CodeGenerator.ValidateIdentifiers(declaration);
            if (constructor.Statements.Count == 0)
            {
                declaration.Members.Remove(constructor);
            }
            return(declaration);
        }
        private void AddDefaultValueAttribute(CodeMemberField field, CodeAttributeDeclarationCollection metadata, object defaultValue, TypeMapping mapping, CodeCommentStatementCollection comments, TypeDesc memberTypeDesc, Accessor accessor, CodeConstructor ctor)
        {
            string str = accessor.IsFixed ? "fixed" : "default";

            if (!memberTypeDesc.HasDefaultSupport)
            {
                if ((comments != null) && (defaultValue is string))
                {
                    DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName);
                    CodeExporter.AddWarningComment(comments, Res.GetString("XmlDropAttributeValue", new object[] { str, mapping.TypeName, defaultValue.ToString() }));
                }
            }
            else if (memberTypeDesc.IsArrayLike && (accessor is ElementAccessor))
            {
                if ((comments != null) && (defaultValue is string))
                {
                    DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName);
                    CodeExporter.AddWarningComment(comments, Res.GetString("XmlDropArrayAttributeValue", new object[] { str, defaultValue.ToString(), ((ElementAccessor)accessor).Name }));
                }
            }
            else if ((mapping.TypeDesc.IsMappedType && (field != null)) && (defaultValue is string))
            {
                SchemaImporterExtension extension = mapping.TypeDesc.ExtendedType.Extension;
                CodeExpression          init      = extension.ImportDefaultValue((string)defaultValue, mapping.TypeDesc.FullName);
                if (init != null)
                {
                    if (ctor != null)
                    {
                        AddInitializationStatement(ctor, field, init);
                    }
                    else
                    {
                        field.InitExpression = extension.ImportDefaultValue((string)defaultValue, mapping.TypeDesc.FullName);
                    }
                }
                if (comments != null)
                {
                    DropDefaultAttribute(accessor, comments, mapping.TypeDesc.FullName);
                    if (init == null)
                    {
                        CodeExporter.AddWarningComment(comments, Res.GetString("XmlNotKnownDefaultValue", new object[] { extension.GetType().FullName, str, (string)defaultValue, mapping.TypeName, mapping.Namespace }));
                    }
                }
            }
            else
            {
                object obj2 = null;
                if ((defaultValue is string) || (defaultValue == null))
                {
                    obj2 = this.ImportDefault(mapping, (string)defaultValue);
                }
                if (obj2 != null)
                {
                    if (!(mapping is PrimitiveMapping))
                    {
                        DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName);
                        CodeExporter.AddWarningComment(comments, Res.GetString("XmlDropNonPrimitiveAttributeValue", new object[] { str, defaultValue.ToString() }));
                    }
                    else
                    {
                        PrimitiveMapping mapping2 = (PrimitiveMapping)mapping;
                        if (((comments != null) && !mapping2.TypeDesc.HasDefaultSupport) && mapping2.TypeDesc.IsMappedType)
                        {
                            DropDefaultAttribute(accessor, comments, mapping2.TypeDesc.FullName);
                        }
                        else if (obj2 == DBNull.Value)
                        {
                            if (comments != null)
                            {
                                CodeExporter.AddWarningComment(comments, Res.GetString("XmlDropAttributeValue", new object[] { str, mapping2.TypeName, defaultValue.ToString() }));
                            }
                        }
                        else
                        {
                            CodeAttributeArgument[] arguments      = null;
                            CodeExpression          initExpression = null;
                            if (mapping2.IsList)
                            {
                                object[]         objArray     = (object[])obj2;
                                CodeExpression[] initializers = new CodeExpression[objArray.Length];
                                for (int i = 0; i < objArray.Length; i++)
                                {
                                    this.GetDefaultValueArguments(mapping2, objArray[i], out initializers[i]);
                                }
                                initExpression = new CodeArrayCreateExpression(field.Type, initializers);
                            }
                            else
                            {
                                arguments = this.GetDefaultValueArguments(mapping2, obj2, out initExpression);
                            }
                            if (field != null)
                            {
                                if (ctor != null)
                                {
                                    AddInitializationStatement(ctor, field, initExpression);
                                }
                                else
                                {
                                    field.InitExpression = initExpression;
                                }
                            }
                            if (((arguments != null) && mapping2.TypeDesc.HasDefaultSupport) && (accessor.IsOptional && !accessor.IsFixed))
                            {
                                CodeAttributeDeclaration declaration = new CodeAttributeDeclaration(typeof(DefaultValueAttribute).FullName, arguments);
                                metadata.Add(declaration);
                            }
                            else if (comments != null)
                            {
                                DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName);
                            }
                        }
                    }
                }
            }
        }