Beispiel #1
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);
        }