Esempio n. 1
0
        private XmlTypeMapping ImportEnumMapping(TypeData typeData, string defaultNamespace)
        {
            Type           type           = typeData.Type;
            XmlTypeMapping xmlTypeMapping = this.helper.GetRegisteredClrType(type, this.GetTypeNamespace(typeData, defaultNamespace));

            if (xmlTypeMapping != null)
            {
                return(xmlTypeMapping);
            }
            ReflectionHelper.CheckSerializableType(type, false);
            xmlTypeMapping = this.CreateTypeMapping(typeData, null, defaultNamespace);
            this.helper.RegisterClrType(xmlTypeMapping, type, xmlTypeMapping.Namespace);
            xmlTypeMapping.MultiReferenceType = true;
            string[] names = Enum.GetNames(type);
            EnumMap.EnumMapMember[] array = new EnumMap.EnumMapMember[names.Length];
            for (int i = 0; i < names.Length; i++)
            {
                FieldInfo field            = type.GetField(names[i]);
                string    name             = names[i];
                object[]  customAttributes = field.GetCustomAttributes(typeof(SoapEnumAttribute), false);
                if (customAttributes.Length > 0)
                {
                    name = ((SoapEnumAttribute)customAttributes[0]).Name;
                }
                long value = ((IConvertible)field.GetValue(null)).ToInt64(CultureInfo.InvariantCulture);
                array[i] = new EnumMap.EnumMapMember(XmlConvert.EncodeLocalName(name), names[i], value);
            }
            bool isFlags = type.IsDefined(typeof(FlagsAttribute), false);

            xmlTypeMapping.ObjectMap = new EnumMap(array, isFlags);
            this.ImportTypeMapping(typeof(object), defaultNamespace).DerivedTypes.Add(xmlTypeMapping);
            return(xmlTypeMapping);
        }
Esempio n. 2
0
        private XmlTypeMapping ImportClassMapping(TypeData typeData, string defaultNamespace)
        {
            Type type = typeData.Type;

            if (type.IsValueType)
            {
                throw this.CreateStructException(type);
            }
            if (type == typeof(object))
            {
                defaultNamespace = "http://www.w3.org/2001/XMLSchema";
            }
            ReflectionHelper.CheckSerializableType(type, false);
            XmlTypeMapping xmlTypeMapping = this.helper.GetRegisteredClrType(type, this.GetTypeNamespace(typeData, defaultNamespace));

            if (xmlTypeMapping != null)
            {
                return(xmlTypeMapping);
            }
            xmlTypeMapping = this.CreateTypeMapping(typeData, null, defaultNamespace);
            this.helper.RegisterClrType(xmlTypeMapping, type, xmlTypeMapping.Namespace);
            xmlTypeMapping.MultiReferenceType = true;
            ClassMap classMap = new ClassMap();

            xmlTypeMapping.ObjectMap = classMap;
            ICollection reflectionMembers = this.GetReflectionMembers(type);

            foreach (object obj in reflectionMembers)
            {
                XmlReflectionMember xmlReflectionMember = (XmlReflectionMember)obj;
                if (!xmlReflectionMember.SoapAttributes.SoapIgnore)
                {
                    classMap.AddMember(this.CreateMapMember(xmlReflectionMember, defaultNamespace));
                }
            }
            SoapIncludeAttribute[] array = (SoapIncludeAttribute[])type.GetCustomAttributes(typeof(SoapIncludeAttribute), false);
            for (int i = 0; i < array.Length; i++)
            {
                Type type2 = array[i].Type;
                this.ImportTypeMapping(type2);
            }
            if (type == typeof(object) && this.includedTypes != null)
            {
                foreach (object obj2 in this.includedTypes)
                {
                    Type type3 = (Type)obj2;
                    xmlTypeMapping.DerivedTypes.Add(this.ImportTypeMapping(type3));
                }
            }
            if (type.BaseType != null)
            {
                XmlTypeMapping xmlTypeMapping2 = this.ImportClassMapping(type.BaseType, defaultNamespace);
                if (type.BaseType != typeof(object))
                {
                    xmlTypeMapping.BaseMap = xmlTypeMapping2;
                }
                this.RegisterDerivedMap(xmlTypeMapping2, xmlTypeMapping);
            }
            return(xmlTypeMapping);
        }
        XmlTypeMapping ImportEnumMapping(TypeData typeData, string defaultNamespace)
        {
            Type           type = typeData.Type;
            XmlTypeMapping map  = helper.GetRegisteredClrType(type, GetTypeNamespace(typeData, defaultNamespace));

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

            ReflectionHelper.CheckSerializableType(type, false);

            map = CreateTypeMapping(typeData, null, defaultNamespace);
            helper.RegisterClrType(map, type, map.Namespace);

            map.MultiReferenceType = true;

            string [] names = Enum.GetNames(type);
            EnumMap.EnumMapMember[] members = new EnumMap.EnumMapMember[names.Length];
            for (int n = 0; n < names.Length; n++)
            {
                FieldInfo field   = type.GetField(names[n]);
                string    xmlName = names[n];
                object[]  atts    = field.GetCustomAttributes(typeof(SoapEnumAttribute), false);
                if (atts.Length > 0)
                {
                    xmlName = ((SoapEnumAttribute)atts[0]).Name;
                }
                long value = ((IConvertible)field.GetValue(null)).ToInt64(CultureInfo.InvariantCulture);
                members[n] = new EnumMap.EnumMapMember(XmlConvert.EncodeLocalName(xmlName), names[n], value);
            }

            bool isFlags = type.IsDefined(typeof(FlagsAttribute), false);

            map.ObjectMap = new EnumMap(members, isFlags);
            ImportTypeMapping(typeof(object), defaultNamespace).DerivedTypes.Add(map);
            return(map);
        }
Esempio n. 4
0
        XmlTypeMapping ImportListMapping(Type type, XmlRootAttribute root, string defaultNamespace, XmlAttributes atts, int nestingLevel)
        {
            TypeData typeData = TypeTranslator.GetTypeData(type);
            ListMap  obmap    = new ListMap();

            if (!allowPrivateTypes)
            {
                ReflectionHelper.CheckSerializableType(type);
            }

            if (atts == null)
            {
                atts = new XmlAttributes();
            }
            Type itemType = typeData.ListItemType;

            // warning: byte[][] should not be considered multiarray
            bool isMultiArray = (type.IsArray && (TypeTranslator.GetTypeData(itemType).SchemaType == SchemaTypes.Array) && itemType.IsArray);

            XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();

            foreach (XmlArrayItemAttribute att in atts.XmlArrayItems)
            {
                if (att.NestingLevel != nestingLevel)
                {
                    continue;
                }
                Type elemType = (att.Type != null) ? att.Type : itemType;
                XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo(null, TypeTranslator.GetTypeData(elemType, att.DataType));
                elem.Namespace = att.Namespace != null ? att.Namespace : defaultNamespace;
                if (elem.Namespace == null)
                {
                    elem.Namespace = "";
                }
                elem.Form         = att.Form;
                elem.IsNullable   = att.IsNullable && CanBeNull(elem.TypeData);
                elem.NestingLevel = att.NestingLevel;

                if (isMultiArray)
                {
                    elem.MappedType = ImportListMapping(elemType, null, elem.Namespace, atts, nestingLevel + 1);
                }
                else if (elem.TypeData.IsComplexType)
                {
                    elem.MappedType = ImportTypeMapping(elemType, null, elem.Namespace);
                }

                if (att.ElementName != null)
                {
                    elem.ElementName = att.ElementName;
                }
                else if (elem.MappedType != null)
                {
                    elem.ElementName = elem.MappedType.ElementName;
                }
                else
                {
                    elem.ElementName = TypeTranslator.GetTypeData(elemType).XmlType;
                }

                list.Add(elem);
            }

            if (list.Count == 0)
            {
                XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo(null, TypeTranslator.GetTypeData(itemType));
                if (isMultiArray)
                {
                    elem.MappedType = ImportListMapping(itemType, null, defaultNamespace, atts, nestingLevel + 1);
                }
                else if (elem.TypeData.IsComplexType)
                {
                    elem.MappedType = ImportTypeMapping(itemType, null, defaultNamespace);
                }

                if (elem.MappedType != null)
                {
                    elem.ElementName = elem.MappedType.XmlType;
                }
                else
                {
                    elem.ElementName = TypeTranslator.GetTypeData(itemType).XmlType;
                }

                elem.Namespace  = (defaultNamespace != null) ? defaultNamespace : "";
                elem.IsNullable = CanBeNull(elem.TypeData);
                list.Add(elem);
            }

            obmap.ItemInfo = list;

            // If there can be different element names (types) in the array, then its name cannot
            // be "ArrayOfXXX" it must be something like ArrayOfChoiceNNN

            string baseName;

            if (list.Count > 1)
            {
                baseName = "ArrayOfChoice" + (arrayChoiceCount++);
            }
            else
            {
                XmlTypeMapElementInfo elem = ((XmlTypeMapElementInfo)list[0]);
                if (elem.MappedType != null)
                {
                    baseName = TypeTranslator.GetArrayName(elem.MappedType.XmlType);
                }
                else
                {
                    baseName = TypeTranslator.GetArrayName(elem.ElementName);
                }
            }

            // Avoid name colisions

            int    nameCount = 1;
            string name      = baseName;

            do
            {
                XmlTypeMapping foundMap = helper.GetRegisteredSchemaType(name, defaultNamespace);
                if (foundMap == null)
                {
                    nameCount = -1;
                }
                else if (obmap.Equals(foundMap.ObjectMap) && typeData.Type == foundMap.TypeData.Type)
                {
                    return(foundMap);
                }
                else
                {
                    name = baseName + (nameCount++);
                }
            }while (nameCount != -1);

            XmlTypeMapping map = CreateTypeMapping(typeData, root, name, defaultNamespace);

            map.ObjectMap = obmap;

            // Register any of the including types as a derived class of object
            XmlIncludeAttribute[] includes = (XmlIncludeAttribute[])type.GetCustomAttributes(typeof(XmlIncludeAttribute), false);

            XmlTypeMapping objectMapping = ImportTypeMapping(typeof(object));

            for (int i = 0; i < includes.Length; i++)
            {
                Type includedType = includes[i].Type;
                objectMapping.DerivedTypes.Add(ImportTypeMapping(includedType, null, defaultNamespace));
            }

            // Register this map as a derived class of object

            helper.RegisterSchemaType(map, name, defaultNamespace);
            ImportTypeMapping(typeof(object)).DerivedTypes.Add(map);

            return(map);
        }
Esempio n. 5
0
        XmlTypeMapping ImportClassMapping(Type type, XmlRootAttribute root, string defaultNamespace)
        {
            TypeData       typeData = TypeTranslator.GetTypeData(type);
            XmlTypeMapping map      = helper.GetRegisteredClrType(type, GetTypeNamespace(typeData, root, defaultNamespace));

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

            if (!allowPrivateTypes)
            {
                ReflectionHelper.CheckSerializableType(type);
            }

            map = CreateTypeMapping(typeData, root, null, defaultNamespace);
            helper.RegisterClrType(map, type, map.XmlTypeNamespace);
            helper.RegisterSchemaType(map, map.XmlType, map.XmlTypeNamespace);

            // Import members

            ClassMap classMap = new ClassMap();

            map.ObjectMap = classMap;

//			try
//			{
            ICollection members = GetReflectionMembers(type);

            foreach (XmlReflectionMember rmember in members)
            {
                if (rmember.XmlAttributes.XmlIgnore)
                {
                    continue;
                }
                XmlTypeMapMember mem = CreateMapMember(rmember, map.XmlTypeNamespace);
                mem.CheckOptionalValueType(type);
                classMap.AddMember(mem);
            }
//			}
//			catch (Exception ex) {
//				throw helper.CreateError (map, ex.Message);
//			}

            ImportIncludedTypes(type, defaultNamespace);

            // Import extra classes

            if (type == typeof(object) && includedTypes != null)
            {
                foreach (Type intype in includedTypes)
                {
                    map.DerivedTypes.Add(ImportTypeMapping(intype, defaultNamespace));
                }
            }

            // Register inheritance relations

            if (type.BaseType != null)
            {
                XmlTypeMapping bmap = ImportClassMapping(type.BaseType, root, defaultNamespace);

                if (type.BaseType != typeof(object))
                {
                    map.BaseMap = bmap;
                    classMap.SetCanBeSimpleType(false);
                }

                // At this point, derived classes of this map must be already registered

                RegisterDerivedMap(bmap, map);

                if (((ClassMap)bmap.ObjectMap).HasSimpleContent && classMap.ElementMembers != null && classMap.ElementMembers.Count != 1)
                {
                    throw new InvalidOperationException(String.Format(errSimple, map.TypeData.TypeName, map.BaseMap.TypeData.TypeName));
                }
            }

            if (classMap.XmlTextCollector != null && !classMap.HasSimpleContent)
            {
                XmlTypeMapMember mem = classMap.XmlTextCollector;
                if (mem.TypeData.Type != typeof(string) &&
                    mem.TypeData.Type != typeof(string[]) &&
                    mem.TypeData.Type != typeof(object[]) &&
                    mem.TypeData.Type != typeof(XmlNode[]))
                {
                    throw new InvalidOperationException(String.Format(errSimple2, map.TypeData.TypeName, mem.Name, mem.TypeData.TypeName));
                }
            }

            return(map);
        }
        XmlTypeMapping ImportClassMapping(TypeData typeData, string defaultNamespace)
        {
            Type type = typeData.Type;

            if (type.IsValueType)
            {
                throw CreateStructException(type);
            }

            if (type == typeof(object))
            {
                defaultNamespace = XmlSchema.Namespace;
            }

            ReflectionHelper.CheckSerializableType(type, false);
            XmlTypeMapping map = helper.GetRegisteredClrType(type, GetTypeNamespace(typeData, defaultNamespace));

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

            map = CreateTypeMapping(typeData, null, defaultNamespace);
            helper.RegisterClrType(map, type, map.Namespace);
            map.MultiReferenceType = true;

            ClassMap classMap = new ClassMap();

            map.ObjectMap = classMap;

            // Import members

            ICollection members = GetReflectionMembers(type);

            foreach (XmlReflectionMember rmember in members)
            {
                if (rmember.SoapAttributes.SoapIgnore)
                {
                    continue;
                }
                classMap.AddMember(CreateMapMember(rmember, defaultNamespace));
            }

            // Import included classes

            SoapIncludeAttribute[] includes = (SoapIncludeAttribute[])type.GetCustomAttributes(typeof(SoapIncludeAttribute), false);
            for (int n = 0; n < includes.Length; n++)
            {
                Type includedType = includes[n].Type;
                ImportTypeMapping(includedType);
            }

            if (type == typeof(object) && includedTypes != null)
            {
                foreach (Type intype in includedTypes)
                {
                    map.DerivedTypes.Add(ImportTypeMapping(intype));
                }
            }

            // Register inheritance relations

            if (type.BaseType != null)
            {
                XmlTypeMapping bmap = ImportClassMapping(type.BaseType, defaultNamespace);

                if (type.BaseType != typeof(object))
                {
                    map.BaseMap = bmap;
                }

                // At this point, derived classes of this map must be already registered

                RegisterDerivedMap(bmap, map);
            }

            return(map);
        }