Exemple #1
0
        void ExportClassCode(XmlTypeMapping map, bool isTopLevel)
        {
            CodeTypeDeclaration codeClass;

            if (IsMapExported(map))
            {
                codeClass = GetMapDeclaration(map);
                if (codeClass != null)
                {
                    // Regenerate attributes, since things may have changed
                    codeClass.CustomAttributes.Clear();
                    AddClassAttributes(codeClass);
                    GenerateClass(map, codeClass, isTopLevel);
                    ExportDerivedTypeAttributes(map, codeClass);
                }
                return;
            }

            if (map.TypeData.Type == typeof(object))
            {
                exportedAnyType = map;
                SetMapExported(map, null);
                foreach (XmlTypeMapping dmap in exportedAnyType.DerivedTypes)
                {
                    if (IsMapExported(dmap) || !dmap.IncludeInSchema)
                    {
                        continue;
                    }
                    ExportTypeMapping(dmap, false);
                    AddInclude(dmap);
                }
                return;
            }

            codeClass = new CodeTypeDeclaration(map.TypeData.TypeName);
            SetMapExported(map, codeClass);

            AddCodeType(codeClass, map.Documentation);
            codeClass.Attributes = MemberAttributes.Public;

            codeClass.IsPartial = CodeProvider.Supports(GeneratorSupport.PartialTypes);
            AddClassAttributes(codeClass);

            GenerateClass(map, codeClass, isTopLevel);
            ExportDerivedTypeAttributes(map, codeClass);

            ExportMembersMapCode(codeClass, (ClassMap)map.ObjectMap, map.XmlTypeNamespace, map.BaseMap);

            if (map.BaseMap != null && map.BaseMap.TypeData.SchemaType != SchemaTypes.XmlNode)
            {
                CodeTypeReference ctr = GetDomType(map.BaseMap.TypeData, false);
                codeClass.BaseTypes.Add(ctr);
                if (map.BaseMap.IncludeInSchema)
                {
                    ExportMapCode(map.BaseMap, false);
                    AddInclude(map.BaseMap);
                }
            }
            ExportDerivedTypes(map, codeClass);
        }
        private void ExportMetadata(CodeAttributeDeclarationCollection metadata, Type attributeType, string name, string ns, TypeDesc typeDesc, TypeDesc dataTypeDesc, object isNullable, XmlSchemaForm form, int nestingLevel, int sequenceId)
        {
            CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(attributeType.FullName);

            if (name != null)
            {
                attribute.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(name)));
            }
            if (typeDesc != null)
            {
                if (isNullable != null && (bool)isNullable && typeDesc.IsValueType && !typeDesc.IsMappedType && CodeProvider.Supports(GeneratorSupport.GenericTypeReference))
                {
                    attribute.Arguments.Add(new CodeAttributeArgument(new CodeTypeOfExpression("System.Nullable`1[" + typeDesc.FullName + "]")));
                    isNullable = null;
                }
                else
                {
                    attribute.Arguments.Add(new CodeAttributeArgument(new CodeTypeOfExpression(typeDesc.FullName)));
                }
            }
            if (form != XmlSchemaForm.None)
            {
                attribute.Arguments.Add(new CodeAttributeArgument("Form", new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(XmlSchemaForm).FullName), Enum.Format(typeof(XmlSchemaForm), form, "G"))));

                if (form == XmlSchemaForm.Unqualified && ns != null && ns.Length == 0)
                {
                    ns = null;
                }
            }
            if (ns != null)
            {
                attribute.Arguments.Add(new CodeAttributeArgument("Namespace", new CodePrimitiveExpression(ns)));
            }
            if (dataTypeDesc != null && dataTypeDesc.IsAmbiguousDataType && !dataTypeDesc.IsMappedType)
            {
                attribute.Arguments.Add(new CodeAttributeArgument("DataType", new CodePrimitiveExpression(dataTypeDesc.DataType.Name)));
            }
            if (isNullable != null)
            {
                attribute.Arguments.Add(new CodeAttributeArgument("IsNullable", new CodePrimitiveExpression((bool)isNullable)));
            }
            if (nestingLevel > 0)
            {
                attribute.Arguments.Add(new CodeAttributeArgument("NestingLevel", new CodePrimitiveExpression(nestingLevel)));
            }
            if (sequenceId >= 0)
            {
                attribute.Arguments.Add(new CodeAttributeArgument("Order", new CodePrimitiveExpression(sequenceId)));
            }
            if (attribute.Arguments.Count == 0 && attributeType == typeof(XmlElementAttribute))
            {
                return;
            }
            metadata.Add(attribute);
        }
        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 CodeTypeDeclaration ExportStruct(StructMapping mapping)
        {
            if (mapping.TypeDesc.IsRoot)
            {
                ExportRoot(mapping, typeof(XmlIncludeAttribute));
                return(null);
            }

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

            CodeTypeDeclaration codeClass = new CodeTypeDeclaration(className);

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

            CodeNamespace.Types.Add(codeClass);

            CodeConstructor ctor = new CodeConstructor();

            ctor.Attributes = (ctor.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            codeClass.Members.Add(ctor);
            if (mapping.TypeDesc.IsAbstract)
            {
                ctor.Attributes |= MemberAttributes.Abstract;
            }

            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;
            }

            AddIncludeMetadata(codeClass.CustomAttributes, mapping, typeof(XmlIncludeAttribute));

            if (mapping.IsSequence)
            {
                int generatedSequence = 0;
                for (int i = 0; i < mapping.Members.Length; i++)
                {
                    MemberMapping member = mapping.Members[i];
                    if (member.IsParticle && member.SequenceId < 0)
                    {
                        member.SequenceId = generatedSequence++;
                    }
                }
            }

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

            for (int i = 0; i < mapping.Members.Length; i++)
            {
                if (mapping.Members[i].Xmlns != null)
                {
                    continue;
                }
                EnsureTypesExported(mapping.Members[i].Elements, mapping.Namespace);
                EnsureTypesExported(mapping.Members[i].Attribute, mapping.Namespace);
                EnsureTypesExported(mapping.Members[i].Text, mapping.Namespace);
            }

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

            ExportDerivedStructs(mapping);
            CodeGenerator.ValidateIdentifiers(codeClass);
            if (ctor.Statements.Count == 0)
            {
                codeClass.Members.Remove(ctor);
            }
            return(codeClass);
        }