Exemplo n.º 1
0
        XmlTypeMapping ImportListMapping(TypeData typeData, string defaultNamespace)
        {
            Type type = typeData.Type;

            XmlTypeMapping map = helper.GetRegisteredClrType(type, XmlSerializer.EncodingNamespace);

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

            ListMap  obmap        = new ListMap();
            TypeData itemTypeData = typeData.ListItemTypeData;

            map = CreateTypeMapping(typeData, "Array", XmlSerializer.EncodingNamespace);
            helper.RegisterClrType(map, type, XmlSerializer.EncodingNamespace);
            map.MultiReferenceType = true;
            map.ObjectMap          = obmap;

            XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo(null, itemTypeData);

            if (elem.TypeData.IsComplexType)
            {
                elem.MappedType = ImportTypeMapping(typeData.ListItemType, defaultNamespace);
                elem.TypeData   = elem.MappedType.TypeData;
            }

            elem.ElementName = "Item";
            elem.Namespace   = string.Empty;
            elem.IsNullable  = true;            // By default, items are nullable

            XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();

            list.Add(elem);

            obmap.ItemInfo = list;
            XmlTypeMapping objMap = ImportTypeMapping(typeof(object), defaultNamespace);

            objMap.DerivedTypes.Add(map);

            // Register any of the including types as a derived class of object
            SoapIncludeAttribute[] includes = (SoapIncludeAttribute[])type.GetCustomAttributes(typeof(SoapIncludeAttribute), false);
            for (int i = 0; i < includes.Length; i++)
            {
                Type includedType = includes[i].Type;
                objMap.DerivedTypes.Add(ImportTypeMapping(includedType, defaultNamespace));
            }

            return(map);
        }
Exemplo n.º 2
0
        private XmlTypeMapMember CreateMapMember(XmlReflectionMember rmember, string defaultNamespace)
        {
            XmlTypeMapMember mapMember;
            SoapAttributes   atts     = rmember.SoapAttributes;
            TypeData         typeData = TypeTranslator.GetTypeData(rmember.MemberType);

            if (atts.SoapAttribute != null)
            {
                // An attribute

                if (typeData.SchemaType != SchemaTypes.Enum && typeData.SchemaType != SchemaTypes.Primitive)
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
                                                                      "Cannot serialize member '{0}' of type {1}. " +
                                                                      "SoapAttribute cannot be used to encode complex types.",
                                                                      rmember.MemberName, typeData.FullTypeName));
                }

                if (atts.SoapElement != null)
                {
                    throw new Exception("SoapAttributeAttribute and SoapElementAttribute cannot be applied to the same member");
                }

                XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute();
                if (atts.SoapAttribute.AttributeName.Length == 0)
                {
                    mapAttribute.AttributeName = XmlConvert.EncodeLocalName(rmember.MemberName);
                }
                else
                {
                    mapAttribute.AttributeName = XmlConvert.EncodeLocalName(atts.SoapAttribute.AttributeName);
                }

                mapAttribute.Namespace = (atts.SoapAttribute.Namespace != null) ? atts.SoapAttribute.Namespace : "";
                if (typeData.IsComplexType)
                {
                    mapAttribute.MappedType = ImportTypeMapping(typeData.Type, defaultNamespace);
                }

                typeData  = TypeTranslator.GetTypeData(rmember.MemberType, atts.SoapAttribute.DataType);
                mapMember = mapAttribute;
                mapMember.DefaultValue = GetDefaultValue(typeData, atts.SoapDefaultValue);
            }
            else
            {
                if (typeData.SchemaType == SchemaTypes.Array)
                {
                    mapMember = new XmlTypeMapMemberList();
                }
                else
                {
                    mapMember = new XmlTypeMapMemberElement();
                }

                if (atts.SoapElement != null && atts.SoapElement.DataType.Length != 0)
                {
                    typeData = TypeTranslator.GetTypeData(rmember.MemberType, atts.SoapElement.DataType);
                }

                // Creates an ElementInfo that identifies the element
                XmlTypeMapElementInfoList infoList = new XmlTypeMapElementInfoList();
                XmlTypeMapElementInfo     elem     = new XmlTypeMapElementInfo(mapMember, typeData);

                elem.ElementName = XmlConvert.EncodeLocalName((atts.SoapElement != null && atts.SoapElement.ElementName.Length != 0) ? atts.SoapElement.ElementName : rmember.MemberName);
                elem.Namespace   = string.Empty;
                elem.IsNullable  = (atts.SoapElement != null) ? atts.SoapElement.IsNullable : false;
                if (typeData.IsComplexType)
                {
                    elem.MappedType = ImportTypeMapping(typeData.Type, defaultNamespace);
                }

                infoList.Add(elem);
                ((XmlTypeMapMemberElement)mapMember).ElementInfo = infoList;
            }

            mapMember.TypeData      = typeData;
            mapMember.Name          = rmember.MemberName;
            mapMember.IsReturnValue = rmember.IsReturnValue;
            return(mapMember);
        }