示例#1
0
        internal CodeTypeDeclaration ExportEnum(EnumMapping mapping, Type type)
        {
            CodeTypeDeclaration codeClass = new CodeTypeDeclaration(mapping.TypeDesc.Name);

            codeClass.Comments.Add(new CodeCommentStatement(ResXml.XmlRemarks, true));
            codeClass.IsEnum = true;
            if (mapping.IsFlags && mapping.Constants.Length > 31)
            {
                codeClass.BaseTypes.Add(new CodeTypeReference(typeof(long)));
            }
            codeClass.TypeAttributes |= TypeAttributes.Public;
            CodeNamespace.Types.Add(codeClass);
            for (int i = 0; i < mapping.Constants.Length; i++)
            {
                ExportConstant(codeClass, mapping.Constants[i], type, mapping.IsFlags, 1L << i);
            }
            if (mapping.IsFlags)
            {
                // Add [FlagsAttribute]
                CodeAttributeDeclaration flags = new CodeAttributeDeclaration(typeof(FlagsAttribute).FullName);
                codeClass.CustomAttributes.Add(flags);
            }
            CodeGenerator.ValidateIdentifiers(codeClass);
            return(codeClass);
        }
        private EnumMapping ImportEnumMapping(EnumModel model)
        {
            SoapAttributes a      = GetAttributes(model.Type);
            string         typeNs = _defaultNs;

            if (a.SoapType != null && a.SoapType.Namespace != null)
            {
                typeNs = a.SoapType.Namespace;
            }
            string typeName = XsdTypeName(model.Type, a, model.TypeDesc.Name);

            typeName = XmlConvert.EncodeLocalName(typeName);

            EnumMapping mapping = (EnumMapping)GetTypeMapping(typeName, typeNs, model.TypeDesc);

            if (mapping == null)
            {
                mapping           = new EnumMapping();
                mapping.IsSoap    = true;
                mapping.TypeDesc  = model.TypeDesc;
                mapping.TypeName  = typeName;
                mapping.Namespace = typeNs;
                mapping.IsFlags   = model.Type.GetTypeInfo().IsDefined(typeof(FlagsAttribute), false);
                _typeScope.AddTypeMapping(mapping);
                _types.Add(typeName, typeNs, mapping);
                ArrayList constants = new ArrayList();
                for (int i = 0; i < model.Constants.Length; i++)
                {
                    ConstantMapping constant = ImportConstantMapping(model.Constants[i]);
                    if (constant != null)
                    {
                        constants.Add(constant);
                    }
                }
                if (constants.Count == 0)
                {
                    throw new InvalidOperationException(string.Format(ResXml.XmlNoSerializableMembers, model.TypeDesc.FullName));
                }
                mapping.Constants = (ConstantMapping[])constants.ToArray(typeof(ConstantMapping));
            }
            return(mapping);
        }
示例#3
0
        private XmlQualifiedName ExportEnumMapping(EnumMapping mapping, string ns)
        {
            XmlSchemaSimpleType dataType = (XmlSchemaSimpleType)_types[mapping];

            if (dataType == null)
            {
                CheckForDuplicateType(mapping.TypeName, mapping.Namespace);
                dataType      = new XmlSchemaSimpleType();
                dataType.Name = mapping.TypeName;
                _types.Add(mapping, dataType);
                AddSchemaItem(dataType, mapping.Namespace, ns);

                XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction();
                restriction.BaseTypeName = new XmlQualifiedName("string", XmlSchema.Namespace);

                for (int i = 0; i < mapping.Constants.Length; i++)
                {
                    ConstantMapping           constant    = mapping.Constants[i];
                    XmlSchemaEnumerationFacet enumeration = new XmlSchemaEnumerationFacet();
                    enumeration.Value = constant.XmlName;
                    restriction.Facets.Add(enumeration);
                }
                if (!mapping.IsFlags)
                {
                    dataType.Content = restriction;
                }
                else
                {
                    XmlSchemaSimpleTypeList list     = new XmlSchemaSimpleTypeList();
                    XmlSchemaSimpleType     enumType = new XmlSchemaSimpleType();
                    enumType.Content = restriction;
                    list.ItemType    = enumType;
                    dataType.Content = list;
                }
            }
            else
            {
                AddSchemaImport(mapping.Namespace, ns);
            }
            return(new XmlQualifiedName(mapping.TypeName, mapping.Namespace));
        }
示例#4
0
        private TypeMapping ImportEnumeratedDataType(XmlSchemaSimpleType dataType, string typeNs, string identifier, bool isList)
        {
            TypeMapping mapping = (TypeMapping)ImportedMappings[dataType];

            if (mapping != null)
            {
                return(mapping);
            }

            XmlSchemaSimpleType sourceDataType = FindDataType(dataType.DerivedFrom);
            TypeDesc            sourceTypeDesc = Scope.GetTypeDesc(sourceDataType);

            if (sourceTypeDesc != null && sourceTypeDesc != Scope.GetTypeDesc(typeof(string)))
            {
                return(ImportPrimitiveDataType(dataType));
            }
            identifier = Accessor.UnescapeName(identifier);
            string      typeName    = GenerateUniqueTypeName(identifier);
            EnumMapping enumMapping = new EnumMapping();

            enumMapping.IsReference = Schemas.IsReference(dataType);
            enumMapping.TypeDesc    = new TypeDesc(typeName, typeName, TypeKind.Enum, null, 0);
            enumMapping.TypeName    = identifier;
            enumMapping.Namespace   = typeNs;
            enumMapping.IsFlags     = isList;
            CodeIdentifiers constants = new CodeIdentifiers();

            if (!(dataType.Content is XmlSchemaSimpleTypeRestriction))
            {
                throw new InvalidOperationException(string.Format(ResXml.XmlInvalidEnumContent, dataType.Content.GetType().Name, identifier));
            }

            XmlSchemaSimpleTypeRestriction restriction = (XmlSchemaSimpleTypeRestriction)dataType.Content;

            for (int i = 0; i < restriction.Facets.Count; i++)
            {
                object facet = restriction.Facets[i];
                if (!(facet is XmlSchemaEnumerationFacet))
                {
                    continue;
                }
                XmlSchemaEnumerationFacet enumeration = (XmlSchemaEnumerationFacet)facet;
                ConstantMapping           constant    = new ConstantMapping();
                string constantName = CodeIdentifier.MakeValid(enumeration.Value);
                constant.Name    = constants.AddUnique(constantName, constant);
                constant.XmlName = enumeration.Value;
                constant.Value   = i;
            }
            enumMapping.Constants = (ConstantMapping[])constants.ToArray(typeof(ConstantMapping));
            if (isList && enumMapping.Constants.Length > 63)
            {
                // if we have 64+ flag constants we cannot map the type to long enum, we will use string mapping instead.
                mapping          = new PrimitiveMapping();
                mapping.TypeDesc = Scope.GetTypeDesc(typeof(string));
                mapping.TypeName = mapping.TypeDesc.DataType.Name;
                ImportedMappings.Add(dataType, mapping);
                return(mapping);
            }
            ImportedMappings.Add(dataType, enumMapping);
            Scope.AddTypeMapping(enumMapping);
            return(enumMapping);
        }
示例#5
0
        private object ImportDefaultValue(TypeMapping mapping, string defaultValue)
        {
            if (defaultValue == null)
            {
                return(null);
            }
            if (!(mapping is PrimitiveMapping))
            {
                return(DBNull.Value);
            }

            if (mapping is EnumMapping)
            {
                EnumMapping       em = (EnumMapping)mapping;
                ConstantMapping[] c  = em.Constants;

                if (em.IsFlags)
                {
                    Hashtable values = new Hashtable();
                    string[]  names  = new string[c.Length];
                    long[]    ids    = new long[c.Length];

                    for (int i = 0; i < c.Length; i++)
                    {
                        ids[i]   = em.IsFlags ? 1L << i : (long)i;
                        names[i] = c[i].Name;
                        values.Add(c[i].Name, ids[i]);
                    }
                    // this validates the values
                    long val = XmlCustomFormatter.ToEnum(defaultValue, values, em.TypeName, true);
                    return(XmlCustomFormatter.FromEnum(val, names, ids, em.TypeDesc.FullName));
                }
                else
                {
                    for (int i = 0; i < c.Length; i++)
                    {
                        if (c[i].XmlName == defaultValue)
                        {
                            return(c[i].Name);
                        }
                    }
                }
                throw new InvalidOperationException(string.Format(ResXml.XmlInvalidDefaultValue, defaultValue, em.TypeDesc.FullName));
            }

            // Primitive mapping
            PrimitiveMapping pm = (PrimitiveMapping)mapping;

            if (!pm.TypeDesc.HasCustomFormatter)
            {
                if (pm.TypeDesc.FormatterName == "String")
                {
                    return(defaultValue);
                }
                if (pm.TypeDesc.FormatterName == "DateTime")
                {
                    return(XmlCustomFormatter.ToDateTime(defaultValue));
                }

                Type formatter = typeof(XmlConvert);

                MethodInfo format = formatter.GetMethod("To" + pm.TypeDesc.FormatterName, new Type[] { typeof(string) });
                if (format != null)
                {
                    return(format.Invoke(formatter, new Object[] { defaultValue }));
                }
            }
            else
            {
                if (pm.TypeDesc.HasDefaultSupport)
                {
                    return(XmlCustomFormatter.ToDefaultValue(defaultValue, pm.TypeDesc.FormatterName));
                }
            }
            return(DBNull.Value);
        }