XmlTypeMapElementInfoList ImportAnyElementInfo(string defaultNamespace, XmlReflectionMember rmember, XmlTypeMapMemberElement member, XmlAttributes atts) { XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList(); ImportTextElementInfo(list, rmember.MemberType, member, atts); foreach (XmlAnyElementAttribute att in atts.XmlAnyElements) { XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo(member, TypeTranslator.GetTypeData(typeof(XmlElement))); if (att.Name != null && att.Name != string.Empty) { elem.ElementName = att.Name; elem.Namespace = (att.Namespace != null) ? att.Namespace : ""; } else { elem.IsUnnamedAnyElement = true; elem.Namespace = defaultNamespace; if (att.Namespace != null) { throw new InvalidOperationException("The element " + rmember.MemberName + " has been attributed with an XmlAnyElementAttribute and a namespace '" + att.Namespace + "', but no name. When a namespace is supplied, a name is also required. Supply a name or remove the namespace."); } } list.Add(elem); } return(list); }
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); }
private MemberMapping ImportMemberMapping(XmlReflectionMember xmlReflectionMember, string ns, XmlReflectionMember[] xmlReflectionMembers, XmlSchemaForm form, RecursionLimiter limiter) { SoapAttributes soapAttributes = xmlReflectionMember.SoapAttributes; if (soapAttributes.SoapIgnore) { return(null); } MemberMapping accessor = new MemberMapping { IsSoap = true, Name = xmlReflectionMember.MemberName }; bool checkSpecified = XmlReflectionImporter.FindSpecifiedMember(xmlReflectionMember.MemberName, xmlReflectionMembers) != null; FieldModel model = new FieldModel(xmlReflectionMember.MemberName, xmlReflectionMember.MemberType, this.typeScope.GetTypeDesc(xmlReflectionMember.MemberType), checkSpecified, false); accessor.CheckShouldPersist = model.CheckShouldPersist; accessor.CheckSpecified = model.CheckSpecified; accessor.ReadOnly = model.ReadOnly; this.ImportAccessorMapping(accessor, model, soapAttributes, ns, form, limiter); if (xmlReflectionMember.OverrideIsNullable) { accessor.Elements[0].IsNullable = false; } return(accessor); }
private MemberMapping ImportMemberMapping(XmlReflectionMember xmlReflectionMember, string ns, XmlReflectionMember[] xmlReflectionMembers, XmlSchemaForm form, RecursionLimiter limiter) { SoapAttributes a = xmlReflectionMember.SoapAttributes; if (a.SoapIgnore) { return(null); } MemberMapping member = new MemberMapping(); member.IsSoap = true; member.Name = xmlReflectionMember.MemberName; bool checkSpecified = XmlReflectionImporter.FindSpecifiedMember(xmlReflectionMember.MemberName, xmlReflectionMembers) != null; FieldModel model = new FieldModel(xmlReflectionMember.MemberName, xmlReflectionMember.MemberType, _typeScope.GetTypeDesc(xmlReflectionMember.MemberType), checkSpecified, false); member.CheckShouldPersist = model.CheckShouldPersist; member.CheckSpecified = model.CheckSpecified; member.ReadOnly = model.ReadOnly; // || !model.FieldTypeDesc.HasDefaultConstructor; ImportAccessorMapping(member, model, a, ns, form, limiter); if (xmlReflectionMember.OverrideIsNullable) { member.Elements[0].IsNullable = false; } return(member); }
public XmlMembersMapping ImportMembersMapping(string mappingName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool rpc) { Array membersObjects = _wrapperObject.InitializeArray(XmlMappingTypesHelper.XmlReflectionMemberType, members); object[] parameters = new object[] { mappingName, ns, membersObjects, hasWrapperElement, rpc }; object o = _wrapperObject.CallMethod(s_importMembersMapping, parameters); return new XmlMembersMapping(o); }
private XmlReflectionMember CreateReflectionMember (MessagePartDescription partDesc, bool isReturnValue) { XmlReflectionMember m = new XmlReflectionMember (); m.IsReturnValue = isReturnValue; m.MemberName = partDesc.Name; m.MemberType = partDesc.Type; return m; }
private XmlReflectionMember CreateReflectionMember (MessagePartDescription partDesc, bool isReturnValue) { XmlReflectionMember m = new XmlReflectionMember (); m.IsReturnValue = isReturnValue; m.MemberName = partDesc.Name; m.MemberType = partDesc.Type; m.XmlAttributes = partDesc.MemberInfo == null ? new XmlAttributes () : new XmlAttributes (partDesc.MemberInfo); return m; }
private XmlMembersMapping MembersMap (Type t, SoapAttributeOverrides overrides, XmlReflectionMember [] members, bool inContainer, bool writeAccessors) { SoapReflectionImporter ri = new SoapReflectionImporter (overrides); XmlMembersMapping mm = ri.ImportMembersMapping (null, null, members, inContainer, writeAccessors); return mm; }
public void CheckOptionalValueType(XmlReflectionMember[] members) { // Used when reflecting a list of members (e.g. web service parameters) for (int n = 0; n < members.Length; n++) { XmlReflectionMember m = members [n]; if (m.MemberName == Name + "Specified" && m.MemberType == typeof(bool) && m.XmlAttributes.XmlIgnore) { IsOptionalValueType = true; _specifiedGlobalIndex = n; break; } } }
public XmlMembersMapping ImportMembersMapping (string elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool writeAccessors, bool validate) { XmlMemberMapping[] mapping = new XmlMemberMapping[members.Length]; for (int n=0; n<members.Length; n++) { XmlTypeMapMember mapMem = CreateMapMember (members[n], ns); mapping[n] = new XmlMemberMapping (members[n].MemberName, ns, mapMem, true); } XmlMembersMapping mps = new XmlMembersMapping (elementName, ns, hasWrapperElement, writeAccessors, mapping); mps.RelatedMaps = relatedMaps; mps.Format = SerializationFormat.Encoded; mps.Source = new MembersSerializationSource (elementName, hasWrapperElement, members, writeAccessors, false, null, includedTypes); return mps; }
ICollection GetReflectionMembers(Type type) { ArrayList members = new ArrayList(); PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public); foreach (PropertyInfo prop in properties) { if (!prop.CanRead) { continue; } if (!prop.CanWrite && (TypeTranslator.GetTypeData(prop.PropertyType).SchemaType != SchemaTypes.Array || prop.PropertyType.IsArray)) { continue; } SoapAttributes atts = attributeOverrides[type, prop.Name]; if (atts == null) { atts = new SoapAttributes(prop); } if (atts.SoapIgnore) { continue; } XmlReflectionMember member = new XmlReflectionMember(prop.Name, prop.PropertyType, atts); members.Add(member); } FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public); foreach (FieldInfo field in fields) { SoapAttributes atts = attributeOverrides[type, field.Name]; if (atts == null) { atts = new SoapAttributes(field); } if (atts.SoapIgnore) { continue; } XmlReflectionMember member = new XmlReflectionMember(field.Name, field.FieldType, atts); members.Add(member); } return(members); }
MembersMapping ImportMembersMapping(XmlReflectionMember[] xmlReflectionMembers, string ns, bool hasWrapperElement, bool writeAccessors, bool validateWrapperElement) { MembersMapping members = new MembersMapping(); members.TypeDesc = typeScope.GetTypeDesc(typeof(object[])); MemberMapping[] mappings = new MemberMapping[xmlReflectionMembers.Length]; for (int i = 0; i < mappings.Length; i++) { try { XmlReflectionMember member = xmlReflectionMembers[i]; MemberMapping mapping = ImportMemberMapping(member, ns, xmlReflectionMembers, hasWrapperElement ? XmlSchemaForm.Unqualified : XmlSchemaForm.Qualified); if (member.IsReturnValue && writeAccessors) // no special treatment for return values with doc/enc { if (i > 0) { throw new InvalidOperationException(Res.GetString(Res.XmlInvalidReturnPosition)); } mapping.IsReturnValue = true; } mappings[i] = mapping; } catch (Exception e) { if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) { throw; } throw ReflectionException(xmlReflectionMembers[i].MemberName, e); } catch { throw ReflectionException(xmlReflectionMembers[i].MemberName, null); } } members.Members = mappings; members.HasWrapperElement = hasWrapperElement; if (hasWrapperElement) { members.ValidateRpcWrapperElement = validateWrapperElement; } members.WriteAccessors = writeAccessors; members.IsSoap = true; if (hasWrapperElement && !writeAccessors) { members.Namespace = ns; } return(members); }
private MembersMapping ImportMembersMapping(XmlReflectionMember[] xmlReflectionMembers, string ns, bool hasWrapperElement, bool writeAccessors, bool validateWrapperElement, RecursionLimiter limiter) { MembersMapping mapping = new MembersMapping { TypeDesc = this.typeScope.GetTypeDesc(typeof(object[])) }; MemberMapping[] mappingArray = new MemberMapping[xmlReflectionMembers.Length]; for (int i = 0; i < mappingArray.Length; i++) { try { XmlReflectionMember xmlReflectionMember = xmlReflectionMembers[i]; MemberMapping mapping2 = this.ImportMemberMapping(xmlReflectionMember, ns, xmlReflectionMembers, hasWrapperElement ? XmlSchemaForm.Unqualified : XmlSchemaForm.Qualified, limiter); if (xmlReflectionMember.IsReturnValue && writeAccessors) { if (i > 0) { throw new InvalidOperationException(Res.GetString("XmlInvalidReturnPosition")); } mapping2.IsReturnValue = true; } mappingArray[i] = mapping2; } catch (Exception exception) { if (((exception is ThreadAbortException) || (exception is StackOverflowException)) || (exception is OutOfMemoryException)) { throw; } throw this.ReflectionException(xmlReflectionMembers[i].MemberName, exception); } } mapping.Members = mappingArray; mapping.HasWrapperElement = hasWrapperElement; if (hasWrapperElement) { mapping.ValidateRpcWrapperElement = validateWrapperElement; } mapping.WriteAccessors = writeAccessors; mapping.IsSoap = true; if (hasWrapperElement && !writeAccessors) { mapping.Namespace = ns; } return(mapping); }
private ICollection GetReflectionMembers(Type type) { ArrayList arrayList = new ArrayList(); PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public); foreach (PropertyInfo propertyInfo in properties) { if (propertyInfo.CanRead) { if (propertyInfo.CanWrite || (TypeTranslator.GetTypeData(propertyInfo.PropertyType).SchemaType == SchemaTypes.Array && !propertyInfo.PropertyType.IsArray)) { SoapAttributes soapAttributes = this.attributeOverrides[type, propertyInfo.Name]; if (soapAttributes == null) { soapAttributes = new SoapAttributes(propertyInfo); } if (!soapAttributes.SoapIgnore) { XmlReflectionMember value = new XmlReflectionMember(propertyInfo.Name, propertyInfo.PropertyType, soapAttributes); arrayList.Add(value); } } } } FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public); foreach (FieldInfo fieldInfo in fields) { SoapAttributes soapAttributes2 = this.attributeOverrides[type, fieldInfo.Name]; if (soapAttributes2 == null) { soapAttributes2 = new SoapAttributes(fieldInfo); } if (!soapAttributes2.SoapIgnore) { XmlReflectionMember value2 = new XmlReflectionMember(fieldInfo.Name, fieldInfo.FieldType, soapAttributes2); arrayList.Add(value2); } } return(arrayList); }
MemberMapping ImportMemberMapping(XmlReflectionMember xmlReflectionMember, string ns, XmlSchemaForm form) { SoapAttributes a = xmlReflectionMember.SoapAttributes; if (a.SoapIgnore) { return(null); } MemberMapping member = new MemberMapping(); member.IsSoap = true; member.Name = xmlReflectionMember.MemberName; FieldModel model = new FieldModel(xmlReflectionMember.MemberName, xmlReflectionMember.MemberType, typeScope.GetTypeDesc(xmlReflectionMember.MemberType), false, false); member.ReadOnly = model.ReadOnly || !model.FieldTypeDesc.HasDefaultConstructor; ImportAccessorMapping(member, model, a, ns, form); if (xmlReflectionMember.OverrideIsNullable) { member.Elements[0].IsNullable = false; } return(member); }
public void ExportXmlSerializable_NestedClassMapping () { XmlSchemas schemas = new XmlSchemas (); XmlReflectionMember xmlReflectionMember = new XmlReflectionMember (); XmlSchemaExporter xmlSchemaExporter = new XmlSchemaExporter (schemas); XmlReflectionImporter xmlReflectionImporter = new XmlReflectionImporter (); //Export mapping for DataSet1 class. xmlReflectionMember.MemberType = typeof (DataSet1); XmlMembersMapping xmlMembersMapping = xmlReflectionImporter.ImportMembersMapping ("DataSet1Response", "ResponseNamespace", new XmlReflectionMember [] { xmlReflectionMember }, true); xmlSchemaExporter.ExportMembersMapping (xmlMembersMapping); //Export mapping for nested of DataSet1 class. xmlReflectionMember.MemberType = typeof (DataSet1.DataTable1DataTable); xmlMembersMapping = xmlReflectionImporter.ImportMembersMapping ("DataTable1DataTableResponse", "ResponseNamespace", new XmlReflectionMember [] { xmlReflectionMember }, true); xmlSchemaExporter.ExportMembersMapping (xmlMembersMapping); }
XmlTypeMapElementInfoList ImportAnyElementInfo (string defaultNamespace, XmlReflectionMember rmember, XmlTypeMapMemberElement member, XmlAttributes atts) { XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList(); ImportTextElementInfo (list, rmember.MemberType, member, atts, defaultNamespace); #if !MOONLIGHT // no practical anyElement support foreach (XmlAnyElementAttribute att in atts.XmlAnyElements) { XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(typeof(XmlElement))); if (att.Name.Length != 0) { elem.ElementName = XmlConvert.EncodeLocalName(att.Name); elem.Namespace = (att.Namespace != null) ? att.Namespace : ""; } else { elem.IsUnnamedAnyElement = true; elem.Namespace = defaultNamespace; if (att.Namespace != null) throw new InvalidOperationException ("The element " + rmember.MemberName + " has been attributed with an XmlAnyElementAttribute and a namespace '" + att.Namespace + "', but no name. When a namespace is supplied, a name is also required. Supply a name or remove the namespace."); } list.Add (elem); } #endif return list; }
public MembersSerializationSource (string elementName, bool hasWrapperElement, XmlReflectionMember [] members, bool writeAccessors, bool literalFormat, string namspace, ArrayList includedTypes) : base (namspace, includedTypes) { this.elementName = elementName; this.hasWrapperElement = hasWrapperElement; this.writeAccessors = writeAccessors; this.literalFormat = literalFormat; StringBuilder sb = new StringBuilder (); sb.Append (members.Length.ToString(CultureInfo.InvariantCulture)); foreach (XmlReflectionMember mem in members) mem.AddKeyHash (sb); membersHash = sb.ToString(); }
XmlReflectionMember [] BuildResponseReflectionMembers (XmlElementAttribute optional_ns) { ParameterInfo [] output = MethodInfo.OutParameters; bool has_return_value = !(OneWay || MethodInfo.ReturnType == typeof (void)); XmlReflectionMember [] out_members = new XmlReflectionMember [(has_return_value ? 1 : 0) + output.Length]; XmlReflectionMember m; int idx = 0; if (has_return_value) { m = new XmlReflectionMember (); m.IsReturnValue = true; m.MemberName = Name + "Result"; m.MemberType = MethodInfo.ReturnType; m.XmlAttributes = new XmlAttributes (MethodInfo.ReturnTypeCustomAttributeProvider); m.SoapAttributes = new SoapAttributes (MethodInfo.ReturnTypeCustomAttributeProvider); if (optional_ns != null) m.XmlAttributes.XmlElements.Add (optional_ns); idx++; out_members [0] = m; } for (int i = 0; i < output.Length; i++) { m = new XmlReflectionMember (); m.IsReturnValue = false; m.MemberName = output [i].Name; m.MemberType = output [i].ParameterType; m.XmlAttributes = new XmlAttributes (output[i]); m.SoapAttributes = new SoapAttributes (output[i]); if (m.MemberType.IsByRef) m.MemberType = m.MemberType.GetElementType (); if (optional_ns != null) m.XmlAttributes.XmlElements.Add (optional_ns); out_members [i + idx] = m; } return out_members; }
static internal XmlReflectionMember GetXmlReflectionMember(XmlName memberName, XmlName elementName, string ns, Type type, MemberInfo additionalAttributesProvider, bool isMultiple, bool isWrapped) { XmlReflectionMember member = new XmlReflectionMember(); member.MemberName = (memberName ?? elementName).DecodedName; member.MemberType = type; if (member.MemberType.IsByRef) member.MemberType = member.MemberType.GetElementType(); if (isMultiple) member.MemberType = member.MemberType.MakeArrayType(); if (additionalAttributesProvider != null) { member.XmlAttributes = XmlAttributesHelper.CreateXmlAttributes(additionalAttributesProvider); } if (member.XmlAttributes == null) member.XmlAttributes = new XmlAttributes(); else { Type invalidAttributeType = null; if (member.XmlAttributes.XmlAttribute != null) invalidAttributeType = typeof(XmlAttributeAttribute); else if (member.XmlAttributes.XmlAnyAttribute != null && !isWrapped) invalidAttributeType = typeof(XmlAnyAttributeAttribute); else if (member.XmlAttributes.XmlChoiceIdentifier != null) invalidAttributeType = typeof(XmlChoiceIdentifierAttribute); else if (member.XmlAttributes.XmlIgnore) invalidAttributeType = typeof(XmlIgnoreAttribute); else if (member.XmlAttributes.Xmlns) invalidAttributeType = typeof(XmlNamespaceDeclarationsAttribute); else if (member.XmlAttributes.XmlText != null) invalidAttributeType = typeof(XmlTextAttribute); else if (member.XmlAttributes.XmlEnum != null) invalidAttributeType = typeof(XmlEnumAttribute); if (invalidAttributeType != null) throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(isWrapped ? SR.SFxInvalidXmlAttributeInWrapped : SR.SFxInvalidXmlAttributeInBare, invalidAttributeType, elementName.DecodedName))); if (member.XmlAttributes.XmlArray != null && isMultiple) throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.SFxXmlArrayNotAllowedForMultiple, elementName.DecodedName, ns))); } bool isArray = member.MemberType.IsArray; if ((isArray && !isMultiple && member.MemberType != typeof(byte[])) || (!isArray && typeof(IEnumerable).IsAssignableFrom(member.MemberType) && member.MemberType != typeof(string) && !typeof(XmlNode).IsAssignableFrom(member.MemberType) && !typeof(IXmlSerializable).IsAssignableFrom(member.MemberType))) { if (member.XmlAttributes.XmlArray != null) { if (member.XmlAttributes.XmlArray.ElementName == String.Empty) member.XmlAttributes.XmlArray.ElementName = elementName.DecodedName; if (member.XmlAttributes.XmlArray.Namespace == null) member.XmlAttributes.XmlArray.Namespace = ns; } else if (HasNoXmlParameterAttributes(member.XmlAttributes)) { member.XmlAttributes.XmlArray = new XmlArrayAttribute(); member.XmlAttributes.XmlArray.ElementName = elementName.DecodedName; member.XmlAttributes.XmlArray.Namespace = ns; } } else { if (member.XmlAttributes.XmlElements == null || member.XmlAttributes.XmlElements.Count == 0) { if (HasNoXmlParameterAttributes(member.XmlAttributes)) { XmlElementAttribute elementAttribute = new XmlElementAttribute(); elementAttribute.ElementName = elementName.DecodedName; elementAttribute.Namespace = ns; member.XmlAttributes.XmlElements.Add(elementAttribute); } } else { foreach (XmlElementAttribute elementAttribute in member.XmlAttributes.XmlElements) { if (elementAttribute.ElementName == String.Empty) elementAttribute.ElementName = elementName.DecodedName; if (elementAttribute.Namespace == null) elementAttribute.Namespace = ns; } } } return member; }
ICollection GetReflectionMembers(Type type) { // First we want to find the inheritance hierarchy in reverse order. Type currentType = type; ArrayList typeList = new ArrayList(); typeList.Add(currentType); while (currentType != typeof(object)) { currentType = currentType.BaseType; // Read the base type. typeList.Insert(0, currentType); // Insert at 0 to reverse the order. } // Read all Fields via reflection. ArrayList fieldList = new ArrayList(); FieldInfo[] tfields = type.GetFields(BindingFlags.Instance | BindingFlags.Public); currentType = null; int currentIndex = 0; foreach (FieldInfo field in tfields) { // This statement ensures fields are ordered starting from the base type. if (currentType != field.DeclaringType) { currentType = field.DeclaringType; currentIndex = 0; } fieldList.Insert(currentIndex++, field); } // Read all Properties via reflection. ArrayList propList = new ArrayList(); PropertyInfo[] tprops = type.GetProperties(BindingFlags.Instance | BindingFlags.Public); currentType = null; currentIndex = 0; foreach (PropertyInfo prop in tprops) { // This statement ensures properties are ordered starting from the base type. if (currentType != prop.DeclaringType) { currentType = prop.DeclaringType; currentIndex = 0; } if (!prop.CanRead) { continue; } if (!prop.CanWrite && TypeTranslator.GetTypeData(prop.PropertyType).SchemaType != SchemaTypes.Array) { continue; } if (prop.GetIndexParameters().Length > 0) { continue; } propList.Insert(currentIndex++, prop); } ArrayList members = new ArrayList(); int fieldIndex = 0; int propIndex = 0; // We now step through the type hierarchy from the base (object) through // to the supplied class, as each step outputting all Fields, and then // all Properties. This is the exact same ordering as .NET 1.0/1.1. foreach (Type t in typeList) { // Add any fields matching the current DeclaringType. while (fieldIndex < fieldList.Count) { FieldInfo field = (FieldInfo)fieldList[fieldIndex]; if (field.DeclaringType == t) { fieldIndex++; XmlAttributes atts = attributeOverrides[type, field.Name]; if (atts == null) { atts = new XmlAttributes(field); } if (atts.XmlIgnore) { continue; } XmlReflectionMember member = new XmlReflectionMember(field.Name, field.FieldType, atts); members.Add(member); } else { break; } } // Add any properties matching the current DeclaringType. while (propIndex < propList.Count) { PropertyInfo prop = (PropertyInfo)propList[propIndex]; if (prop.DeclaringType == t) { propIndex++; XmlAttributes atts = attributeOverrides[type, prop.Name]; if (atts == null) { atts = new XmlAttributes(prop); } if (atts.XmlIgnore) { continue; } XmlReflectionMember member = new XmlReflectionMember(prop.Name, prop.PropertyType, atts); members.Add(member); } else { break; } } } return(members); }
private XmlTypeMapMember CreateMapMember(XmlReflectionMember rmember, string defaultNamespace) { SoapAttributes soapAttributes = rmember.SoapAttributes; TypeData typeData = TypeTranslator.GetTypeData(rmember.MemberType); XmlTypeMapMember xmlTypeMapMember; if (soapAttributes.SoapAttribute != null) { 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.", new object[] { rmember.MemberName, typeData.FullTypeName })); } if (soapAttributes.SoapElement != null) { throw new Exception("SoapAttributeAttribute and SoapElementAttribute cannot be applied to the same member"); } XmlTypeMapMemberAttribute xmlTypeMapMemberAttribute = new XmlTypeMapMemberAttribute(); if (soapAttributes.SoapAttribute.AttributeName.Length == 0) { xmlTypeMapMemberAttribute.AttributeName = XmlConvert.EncodeLocalName(rmember.MemberName); } else { xmlTypeMapMemberAttribute.AttributeName = XmlConvert.EncodeLocalName(soapAttributes.SoapAttribute.AttributeName); } xmlTypeMapMemberAttribute.Namespace = ((soapAttributes.SoapAttribute.Namespace == null) ? string.Empty : soapAttributes.SoapAttribute.Namespace); if (typeData.IsComplexType) { xmlTypeMapMemberAttribute.MappedType = this.ImportTypeMapping(typeData.Type, defaultNamespace); } typeData = TypeTranslator.GetTypeData(rmember.MemberType, soapAttributes.SoapAttribute.DataType); xmlTypeMapMember = xmlTypeMapMemberAttribute; xmlTypeMapMember.DefaultValue = this.GetDefaultValue(typeData, soapAttributes.SoapDefaultValue); } else { if (typeData.SchemaType == SchemaTypes.Array) { xmlTypeMapMember = new XmlTypeMapMemberList(); } else { xmlTypeMapMember = new XmlTypeMapMemberElement(); } if (soapAttributes.SoapElement != null && soapAttributes.SoapElement.DataType.Length != 0) { typeData = TypeTranslator.GetTypeData(rmember.MemberType, soapAttributes.SoapElement.DataType); } XmlTypeMapElementInfoList xmlTypeMapElementInfoList = new XmlTypeMapElementInfoList(); XmlTypeMapElementInfo xmlTypeMapElementInfo = new XmlTypeMapElementInfo(xmlTypeMapMember, typeData); xmlTypeMapElementInfo.ElementName = XmlConvert.EncodeLocalName((soapAttributes.SoapElement == null || soapAttributes.SoapElement.ElementName.Length == 0) ? rmember.MemberName : soapAttributes.SoapElement.ElementName); xmlTypeMapElementInfo.Namespace = string.Empty; xmlTypeMapElementInfo.IsNullable = (soapAttributes.SoapElement != null && soapAttributes.SoapElement.IsNullable); if (typeData.IsComplexType) { xmlTypeMapElementInfo.MappedType = this.ImportTypeMapping(typeData.Type, defaultNamespace); } xmlTypeMapElementInfoList.Add(xmlTypeMapElementInfo); ((XmlTypeMapMemberElement)xmlTypeMapMember).ElementInfo = xmlTypeMapElementInfoList; } xmlTypeMapMember.TypeData = typeData; xmlTypeMapMember.Name = rmember.MemberName; xmlTypeMapMember.IsReturnValue = rmember.IsReturnValue; return(xmlTypeMapMember); }
internal XmlMembersMapping ImportFaultElement(FaultDescription fault, out XmlQualifiedName elementName) { // the number of reflection members is always 1 because there is only one fault detail type XmlReflectionMember[] members = new XmlReflectionMember[1]; XmlName faultElementName = fault.ElementName; string faultNamespace = fault.Namespace; if (faultElementName == null) { XmlTypeMapping mapping = _parent._importer.ImportTypeMapping(fault.DetailType); faultElementName = new XmlName(mapping.ElementName, false /*isEncoded*/); faultNamespace = mapping.Namespace; if (faultElementName == null) throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.SFxFaultTypeAnonymous, this.Operation.Name, fault.DetailType.FullName))); } elementName = new XmlQualifiedName(faultElementName.DecodedName, faultNamespace); members[0] = XmlSerializerHelper.GetXmlReflectionMember(null /*memberName*/, faultElementName, faultNamespace, fault.DetailType, null /*additionalAttributesProvider*/, false /*isMultiple*/, false /*isWrapped*/); string mappingKey = "fault:" + faultElementName.DecodedName + ":" + faultNamespace; return ImportMembersMapping(faultElementName.EncodedName, faultNamespace, members, false /*hasWrapperElement*/, this.IsRpc, mappingKey); }
internal XmlMembersMapping ImportMembersMapping(XmlName elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool rpc, string mappingKey) { XmlMembersMapping mapping; string mappingName = elementName.DecodedName; if (XmlMappings.TryGetValue(mappingKey, out mapping)) { return mapping; } mapping = this.XmlImporter.ImportMembersMapping(mappingName, ns, members, hasWrapperElement, rpc); mapping.SetKey(mappingKey); XmlMappings.Add(mappingKey, mapping); return mapping; }
private XmlTypeMapMember CreateMapMember(XmlReflectionMember rmember, string defaultNamespace) { XmlTypeMapMember mapMember; XmlAttributes atts = rmember.XmlAttributes; TypeData typeData = TypeTranslator.GetTypeData(rmember.MemberType); if (atts.XmlAnyAttribute != null) { if ((rmember.MemberType.FullName == "System.Xml.XmlAttribute[]") || (rmember.MemberType.FullName == "System.Xml.XmlNode[]")) { mapMember = new XmlTypeMapMemberAnyAttribute(); } else { throw new InvalidOperationException("XmlAnyAttributeAttribute can only be applied to members of type XmlAttribute[] or XmlNode[]"); } } else if (atts.XmlAnyElements != null && atts.XmlAnyElements.Count > 0) { if ((rmember.MemberType.FullName == "System.Xml.XmlElement[]") || (rmember.MemberType.FullName == "System.Xml.XmlNode[]") || (rmember.MemberType.FullName == "System.Xml.XmlElement")) { XmlTypeMapMemberAnyElement member = new XmlTypeMapMemberAnyElement(); member.ElementInfo = ImportAnyElementInfo(defaultNamespace, rmember, member, atts); mapMember = member; } else { throw new InvalidOperationException("XmlAnyElementAttribute can only be applied to members of type XmlElement, XmlElement[] or XmlNode[]"); } } else if (atts.Xmlns) { XmlTypeMapMemberNamespaces mapNamespaces = new XmlTypeMapMemberNamespaces(); mapMember = mapNamespaces; } else if (atts.XmlAttribute != null) { // An attribute if (atts.XmlElements != null && atts.XmlElements.Count > 0) { throw new Exception("XmlAttributeAttribute and XmlElementAttribute cannot be applied to the same member"); } XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute(); if (atts.XmlAttribute.AttributeName == null) { mapAttribute.AttributeName = rmember.MemberName; } else { mapAttribute.AttributeName = atts.XmlAttribute.AttributeName; } if (typeData.IsComplexType) { mapAttribute.MappedType = ImportTypeMapping(typeData.Type, null, mapAttribute.Namespace); } if (atts.XmlAttribute.Namespace != null && atts.XmlAttribute.Namespace != defaultNamespace) { if (atts.XmlAttribute.Form == XmlSchemaForm.Unqualified) { throw new InvalidOperationException("The Form property may not be 'Unqualified' when an explicit Namespace property is present"); } mapAttribute.Form = XmlSchemaForm.Qualified; mapAttribute.Namespace = atts.XmlAttribute.Namespace; } else { mapAttribute.Form = atts.XmlAttribute.Form; if (atts.XmlAttribute.Form == XmlSchemaForm.Qualified) { mapAttribute.Namespace = defaultNamespace; } else { mapAttribute.Namespace = ""; } } typeData = TypeTranslator.GetTypeData(rmember.MemberType, atts.XmlAttribute.DataType); mapMember = mapAttribute; } else if (typeData.SchemaType == SchemaTypes.Array) { // If the member has a single XmlElementAttribute and the type is the type of the member, // then it is not a flat list if (atts.XmlElements.Count > 1 || (atts.XmlElements.Count == 1 && atts.XmlElements[0].Type != typeData.Type) || (atts.XmlText != null)) { // A flat list // TODO: check that it does not have XmlArrayAttribute XmlTypeMapMemberFlatList member = new XmlTypeMapMemberFlatList(); member.ListMap = new ListMap(); member.ListMap.ItemInfo = ImportElementInfo(rmember.MemberName, defaultNamespace, typeData.ListItemType, member, atts); member.ElementInfo = member.ListMap.ItemInfo; mapMember = member; } else { // A list XmlTypeMapMemberList member = new XmlTypeMapMemberList(); // Creates an ElementInfo that identifies the array instance. member.ElementInfo = new XmlTypeMapElementInfoList(); XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo(member, typeData); elem.ElementName = (atts.XmlArray != null && atts.XmlArray.ElementName != null) ? atts.XmlArray.ElementName : rmember.MemberName; elem.Namespace = (atts.XmlArray != null && atts.XmlArray.Namespace != null) ? atts.XmlArray.Namespace : defaultNamespace; elem.MappedType = ImportListMapping(rmember.MemberType, null, elem.Namespace, atts, 0); elem.IsNullable = (atts.XmlArray != null) ? atts.XmlArray.IsNullable : false; elem.Form = (atts.XmlArray != null) ? atts.XmlArray.Form : XmlSchemaForm.Qualified; member.ElementInfo.Add(elem); mapMember = member; } } else { // An element XmlTypeMapMemberElement member = new XmlTypeMapMemberElement(); member.ElementInfo = ImportElementInfo(rmember.MemberName, defaultNamespace, rmember.MemberType, member, atts); mapMember = member; } mapMember.DefaultValue = atts.XmlDefaultValue; mapMember.TypeData = typeData; mapMember.Name = rmember.MemberName; mapMember.IsReturnValue = rmember.IsReturnValue; return(mapMember); }
XmlTypeMapping ImportEnumMapping (TypeData typeData, XmlRootAttribute root, string defaultNamespace) { Type type = typeData.Type; XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, root, defaultNamespace)); if (map != null) return map; if (!allowPrivateTypes) ReflectionHelper.CheckSerializableType (type, false); map = CreateTypeMapping (typeData, root, null, defaultNamespace); map.IsNullable = false; helper.RegisterClrType (map, type, map.XmlTypeNamespace); ArrayList members = new ArrayList(); #if MOONLIGHT foreach (string name in GetEnumNames (type)) { #else string [] names = Enum.GetNames (type); foreach (string name in names) { #endif FieldInfo field = type.GetField (name); string xmlName = null; if (field.IsDefined(typeof(XmlIgnoreAttribute), false)) continue; object[] atts = field.GetCustomAttributes (typeof(XmlEnumAttribute), false); if (atts.Length > 0) xmlName = ((XmlEnumAttribute)atts[0]).Name; if (xmlName == null) xmlName = name; long value = ((IConvertible) field.GetValue (null)).ToInt64 (CultureInfo.InvariantCulture); members.Add (new EnumMap.EnumMapMember (xmlName, name, value)); } bool isFlags = type.IsDefined (typeof (FlagsAttribute), false); map.ObjectMap = new EnumMap ((EnumMap.EnumMapMember[])members.ToArray (typeof(EnumMap.EnumMapMember)), isFlags); ImportTypeMapping (typeof(object)).DerivedTypes.Add (map); return map; } XmlTypeMapping ImportXmlSerializableMapping (TypeData typeData, XmlRootAttribute root, string defaultNamespace) { Type type = typeData.Type; XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, root, defaultNamespace)); if (map != null) return map; if (!allowPrivateTypes) ReflectionHelper.CheckSerializableType (type, false); map = CreateTypeMapping (typeData, root, null, defaultNamespace); helper.RegisterClrType (map, type, map.XmlTypeNamespace); return map; } void ImportIncludedTypes (Type type, string defaultNamespace) { XmlIncludeAttribute[] includes = (XmlIncludeAttribute[])type.GetCustomAttributes (typeof (XmlIncludeAttribute), false); for (int n=0; n<includes.Length; n++) { Type includedType = includes[n].Type; ImportTypeMapping (includedType, null, defaultNamespace); } } ICollection GetReflectionMembers (Type type) { // First we want to find the inheritance hierarchy in reverse order. Type currentType = type; ArrayList typeList = new ArrayList(); typeList.Add(currentType); while (currentType != typeof(object)) { currentType = currentType.BaseType; // Read the base type. typeList.Insert(0, currentType); // Insert at 0 to reverse the order. } // Read all Fields via reflection. ArrayList fieldList = new ArrayList(); FieldInfo[] tfields = type.GetFields (BindingFlags.Instance | BindingFlags.Public); #if TARGET_JVM // This statement ensures fields are ordered starting from the base type. for (int ti=0; ti<typeList.Count; ti++) { for (int i=0; i<tfields.Length; i++) { FieldInfo field = tfields[i]; if (field.DeclaringType == typeList[ti]) fieldList.Add (field); } } #else currentType = null; int currentIndex = 0; foreach (FieldInfo field in tfields) { // This statement ensures fields are ordered starting from the base type. if (currentType != field.DeclaringType) { currentType = field.DeclaringType; currentIndex=0; } fieldList.Insert(currentIndex++, field); } #endif // Read all Properties via reflection. ArrayList propList = new ArrayList(); PropertyInfo[] tprops = type.GetProperties (BindingFlags.Instance | BindingFlags.Public); #if TARGET_JVM // This statement ensures properties are ordered starting from the base type. for (int ti=0; ti<typeList.Count; ti++) { for (int i=0; i<tprops.Length; i++) { PropertyInfo prop = tprops[i]; if (!prop.CanRead) continue; if (prop.GetIndexParameters().Length > 0) continue; if (prop.DeclaringType == typeList[ti]) propList.Add (prop); } } #else currentType = null; currentIndex = 0; foreach (PropertyInfo prop in tprops) { // This statement ensures properties are ordered starting from the base type. if (currentType != prop.DeclaringType) { currentType = prop.DeclaringType; currentIndex = 0; } if (!prop.CanRead) continue; if (prop.GetIndexParameters().Length > 0) continue; propList.Insert(currentIndex++, prop); } #endif ArrayList members = new ArrayList(); int fieldIndex=0; int propIndex=0; // We now step through the type hierarchy from the base (object) through // to the supplied class, as each step outputting all Fields, and then // all Properties. This is the exact same ordering as .NET 1.0/1.1. foreach (Type t in typeList) { // Add any fields matching the current DeclaringType. while (fieldIndex < fieldList.Count) { FieldInfo field = (FieldInfo)fieldList[fieldIndex]; if (field.DeclaringType==t) { fieldIndex++; XmlAttributes atts = attributeOverrides[type, field.Name]; if (atts == null) atts = new XmlAttributes (field); if (atts.XmlIgnore) continue; XmlReflectionMember member = new XmlReflectionMember(field.Name, field.FieldType, atts); member.DeclaringType = field.DeclaringType; members.Add(member); } else break; } // Add any properties matching the current DeclaringType. while (propIndex < propList.Count) { PropertyInfo prop = (PropertyInfo)propList[propIndex]; if (prop.DeclaringType==t) { propIndex++; XmlAttributes atts = attributeOverrides[type, prop.Name]; if (atts == null) atts = new XmlAttributes (prop); if (atts.XmlIgnore) continue; if (!prop.CanWrite && (TypeTranslator.GetTypeData (prop.PropertyType).SchemaType != SchemaTypes.Array || prop.PropertyType.IsArray)) continue; XmlReflectionMember member = new XmlReflectionMember(prop.Name, prop.PropertyType, atts); member.DeclaringType = prop.DeclaringType; members.Add(member); } else break; } } return members; }
public void CheckOptionalValueType (XmlReflectionMember[] members) { // Used when reflecting a list of members (e.g. web service parameters) for (int n=0; n<members.Length; n++) { XmlReflectionMember m = members [n]; if (m.MemberName == Name + "Specified" && m.MemberType == typeof(bool) && m.XmlAttributes.XmlIgnore) { IsOptionalValueType = true; _specifiedGlobalIndex = n; break; } } }
XmlReflectionMember [] BuildRequestReflectionMembers (XmlElementAttribute optional_ns) { ParameterInfo [] input = MethodInfo.InParameters; XmlReflectionMember [] in_members = new XmlReflectionMember [input.Length]; for (int i = 0; i < input.Length; i++) { XmlReflectionMember m = new XmlReflectionMember (); m.IsReturnValue = false; m.MemberName = input [i].Name; m.MemberType = input [i].ParameterType; m.XmlAttributes = new XmlAttributes (input[i]); m.SoapAttributes = new SoapAttributes (input[i]); if (m.MemberType.IsByRef) m.MemberType = m.MemberType.GetElementType (); if (optional_ns != null) m.XmlAttributes.XmlElements.Add (optional_ns); in_members [i] = m; } return in_members; }
/// <include file='doc\SoapReflectionImporter.uex' path='docs/doc[@for="SoapReflectionImporter.ImportMembersMapping"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public XmlMembersMapping ImportMembersMapping(string elementName, string ns, XmlReflectionMember[] members) { return ImportMembersMapping(elementName, ns, members, true, true, false); }
XmlReflectionMember [] BuildHeadersReflectionMembers (HeaderInfo[] headers) { XmlReflectionMember [] mems = new XmlReflectionMember [headers.Length]; for (int n=0; n<headers.Length; n++) { HeaderInfo header = headers [n]; XmlReflectionMember m = new XmlReflectionMember (); m.IsReturnValue = false; m.MemberName = header.HeaderType.Name; m.MemberType = header.HeaderType; // MS.NET reflects header classes in a weird way. The root element // name is the CLR class name unless it is specified in an XmlRootAttribute. // The usual is to use the xml type name by default, but not in this case. XmlAttributes ats = new XmlAttributes (header.HeaderType); if (ats.XmlRoot != null) { XmlElementAttribute xe = new XmlElementAttribute (); xe.ElementName = ats.XmlRoot.ElementName; xe.Namespace = ats.XmlRoot.Namespace; m.XmlAttributes = new XmlAttributes (); m.XmlAttributes.XmlElements.Add (xe); } mems [n] = m; } return mems; }
/// <include file='doc\SoapReflectionImporter.uex' path='docs/doc[@for="SoapReflectionImporter.ImportMembersMapping2"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public XmlMembersMapping ImportMembersMapping(string elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool writeAccessors, bool validate) { return ImportMembersMapping(elementName, ns, members, hasWrapperElement, writeAccessors, validate, XmlMappingAccess.Read | XmlMappingAccess.Write); }
public XmlMembersMapping ImportMembersMapping (string elementName, string ns, XmlReflectionMember [] members, bool hasWrapperElement) { return ImportMembersMapping (elementName, ns, members, hasWrapperElement, true); }
MembersMapping ImportMembersMapping(XmlReflectionMember[] xmlReflectionMembers, string ns, bool hasWrapperElement, bool writeAccessors, bool validateWrapperElement) { MembersMapping members = new MembersMapping(); members.TypeDesc = typeScope.GetTypeDesc(typeof(object[])); MemberMapping[] mappings = new MemberMapping[xmlReflectionMembers.Length]; for (int i = 0; i < mappings.Length; i++) { try { XmlReflectionMember member = xmlReflectionMembers[i]; MemberMapping mapping = ImportMemberMapping(member, ns, xmlReflectionMembers, hasWrapperElement ? XmlSchemaForm.Unqualified : XmlSchemaForm.Qualified); if (member.IsReturnValue && writeAccessors) { // no special treatment for return values with doc/enc if (i > 0) throw new InvalidOperationException(Res.GetString(Res.XmlInvalidReturnPosition)); mapping.IsReturnValue = true; } mappings[i] = mapping; } catch (Exception e) { if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) { throw; } throw ReflectionException(xmlReflectionMembers[i].MemberName, e); } catch { throw ReflectionException(xmlReflectionMembers[i].MemberName, null); } } members.Members = mappings; members.HasWrapperElement = hasWrapperElement; if (hasWrapperElement) { members.ValidateRpcWrapperElement = validateWrapperElement; } members.WriteAccessors = writeAccessors; members.IsSoap = true; if (hasWrapperElement && !writeAccessors) members.Namespace = ns; return members; }
XmlMembersMapping ImportMembersMapping (string elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool writeAccessors, bool validate, XmlMappingAccess access) { // Reset (); Disabled. See ChangeLog ArrayList mapping = new ArrayList (); for (int n=0; n<members.Length; n++) { if (members[n].XmlAttributes.XmlIgnore) continue; XmlTypeMapMember mapMem = CreateMapMember (null, members[n], ns); mapMem.GlobalIndex = n; mapMem.CheckOptionalValueType (members); mapping.Add (new XmlMemberMapping (members[n].MemberName, ns, mapMem, false)); } elementName = XmlConvert.EncodeLocalName (elementName); XmlMembersMapping mps = new XmlMembersMapping (elementName, ns, hasWrapperElement, false, (XmlMemberMapping[])mapping.ToArray (typeof(XmlMemberMapping))); mps.RelatedMaps = relatedMaps; mps.Format = SerializationFormat.Literal; Type[] extraTypes = includedTypes != null ? (Type[])includedTypes.ToArray(typeof(Type)) : null; #if !NET_2_1 mps.Source = new MembersSerializationSource (elementName, hasWrapperElement, members, false, true, ns, extraTypes); if (allowPrivateTypes) mps.Source.CanBeGenerated = false; #endif return mps; }
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); }
private XmlTypeMapMember CreateMapMember (Type declaringType, XmlReflectionMember rmember, string defaultNamespace) { XmlTypeMapMember mapMember; XmlAttributes atts = rmember.XmlAttributes; TypeData typeData = TypeTranslator.GetTypeData (rmember.MemberType); if (atts.XmlArray != null) { if (atts.XmlArray.Namespace != null && atts.XmlArray.Form == XmlSchemaForm.Unqualified) throw new InvalidOperationException ("XmlArrayAttribute.Form must not be Unqualified when it has an explicit Namespace value."); if (typeData.SchemaType != SchemaTypes.Array && !(typeData.SchemaType == SchemaTypes.Primitive && typeData.Type == typeof (byte []))) throw new InvalidOperationException ("XmlArrayAttribute can be applied to members of array or collection type."); } #if !MOONLIGHT if (atts.XmlAnyAttribute != null) { if ( (rmember.MemberType.FullName == "System.Xml.XmlAttribute[]") || (rmember.MemberType.FullName == "System.Xml.XmlNode[]") ) { mapMember = new XmlTypeMapMemberAnyAttribute(); } else throw new InvalidOperationException ("XmlAnyAttributeAttribute can only be applied to members of type XmlAttribute[] or XmlNode[]"); } else #endif if (atts.XmlAnyElements != null && atts.XmlAnyElements.Count > 0) { // no XmlNode type check is done here (seealso: bug #553032). XmlTypeMapMemberAnyElement member = new XmlTypeMapMemberAnyElement(); member.ElementInfo = ImportAnyElementInfo (defaultNamespace, rmember, member, atts); mapMember = member; } else if (atts.Xmlns) { XmlTypeMapMemberNamespaces mapNamespaces = new XmlTypeMapMemberNamespaces (); mapMember = mapNamespaces; } else if (atts.XmlAttribute != null) { // An attribute if (atts.XmlElements != null && atts.XmlElements.Count > 0) throw new Exception ("XmlAttributeAttribute and XmlElementAttribute cannot be applied to the same member"); XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute (); if (atts.XmlAttribute.AttributeName.Length == 0) mapAttribute.AttributeName = rmember.MemberName; else mapAttribute.AttributeName = atts.XmlAttribute.AttributeName; mapAttribute.AttributeName = XmlConvert.EncodeLocalName (mapAttribute.AttributeName); if (typeData.IsComplexType) mapAttribute.MappedType = ImportTypeMapping (typeData.Type, null, defaultNamespace); if (atts.XmlAttribute.Namespace != null && atts.XmlAttribute.Namespace != defaultNamespace) { if (atts.XmlAttribute.Form == XmlSchemaForm.Unqualified) throw new InvalidOperationException ("The Form property may not be 'Unqualified' when an explicit Namespace property is present"); mapAttribute.Form = XmlSchemaForm.Qualified; mapAttribute.Namespace = atts.XmlAttribute.Namespace; } else { mapAttribute.Form = atts.XmlAttribute.Form; if (atts.XmlAttribute.Form == XmlSchemaForm.Qualified) mapAttribute.Namespace = defaultNamespace; else mapAttribute.Namespace = ""; } typeData = TypeTranslator.GetTypeData(rmember.MemberType, atts.XmlAttribute.DataType); mapMember = mapAttribute; } else if (typeData.SchemaType == SchemaTypes.Array) { // If the member has a single XmlElementAttribute and the type is the type of the member, // then it is not a flat list if (atts.XmlElements.Count > 1 || (atts.XmlElements.Count == 1 && atts.XmlElements[0].Type != typeData.Type) || (atts.XmlText != null)) { // A flat list // check that it does not have XmlArrayAttribute if (atts.XmlArray != null) throw new InvalidOperationException ("XmlArrayAttribute cannot be used with members which also attributed with XmlElementAttribute or XmlTextAttribute."); XmlTypeMapMemberFlatList member = new XmlTypeMapMemberFlatList (); member.ListMap = new ListMap (); member.ListMap.ItemInfo = ImportElementInfo (declaringType, XmlConvert.EncodeLocalName (rmember.MemberName), defaultNamespace, typeData.ListItemType, member, atts); member.ElementInfo = member.ListMap.ItemInfo; member.ListMap.ChoiceMember = member.ChoiceMember; mapMember = member; } else { // A list XmlTypeMapMemberList member = new XmlTypeMapMemberList (); // Creates an ElementInfo that identifies the array instance. member.ElementInfo = new XmlTypeMapElementInfoList(); XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, typeData); elem.ElementName = XmlConvert.EncodeLocalName((atts.XmlArray != null && atts.XmlArray.ElementName.Length != 0) ? atts.XmlArray.ElementName : rmember.MemberName); // note that it could be changed below (when Form is Unqualified) elem.Namespace = (atts.XmlArray != null && atts.XmlArray.Namespace != null) ? atts.XmlArray.Namespace : defaultNamespace; elem.MappedType = ImportListMapping (rmember.MemberType, null, elem.Namespace, atts, 0); elem.IsNullable = (atts.XmlArray != null) ? atts.XmlArray.IsNullable : false; elem.Form = (atts.XmlArray != null) ? atts.XmlArray.Form : XmlSchemaForm.Qualified; // This is a bit tricky, but is done // after filling descendant members, so // that array items could be serialized // with proper namespace. if (atts.XmlArray != null && atts.XmlArray.Form == XmlSchemaForm.Unqualified) elem.Namespace = String.Empty; member.ElementInfo.Add (elem); mapMember = member; } } else { // An element XmlTypeMapMemberElement member = new XmlTypeMapMemberElement (); member.ElementInfo = ImportElementInfo (declaringType, XmlConvert.EncodeLocalName(rmember.MemberName), defaultNamespace, rmember.MemberType, member, atts); mapMember = member; } mapMember.DefaultValue = GetDefaultValue (typeData, atts.XmlDefaultValue); mapMember.TypeData = typeData; mapMember.Name = rmember.MemberName; mapMember.IsReturnValue = rmember.IsReturnValue; return mapMember; }
XmlMembersMapping ImportMembersMapping (string elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool writeAccessors, bool validate, XmlMappingAccess access) { elementName = XmlConvert.EncodeLocalName (elementName); XmlMemberMapping[] mapping = new XmlMemberMapping[members.Length]; for (int n=0; n<members.Length; n++) { XmlTypeMapMember mapMem = CreateMapMember (members[n], ns); mapping[n] = new XmlMemberMapping (XmlConvert.EncodeLocalName (members[n].MemberName), ns, mapMem, true); } XmlMembersMapping mps = new XmlMembersMapping (elementName, ns, hasWrapperElement, writeAccessors, mapping); mps.RelatedMaps = relatedMaps; mps.Format = SerializationFormat.Encoded; Type[] extraTypes = includedTypes != null ? (Type[])includedTypes.ToArray(typeof(Type)) : null; mps.Source = new MembersSerializationSource (elementName, hasWrapperElement, members, writeAccessors, false, null, extraTypes); return mps; }
XmlSerializer GetSerializer (MessageBodyDescription desc) { if (bodySerializers.ContainsKey (desc)) return bodySerializers [desc]; int count = desc.Parts.Count + (HasReturnValue (desc) ? 1 : 0); XmlReflectionMember [] members = new XmlReflectionMember [count]; int ind = 0; if (HasReturnValue (desc)) members [ind++] = CreateReflectionMember (desc.ReturnValue, true); foreach (MessagePartDescription partDesc in desc.Parts) members [ind++] = CreateReflectionMember (partDesc, false); XmlReflectionImporter xmlImporter = new XmlReflectionImporter (); // Register known types into xmlImporter. foreach (var type in OperationKnownTypes) xmlImporter.IncludeType (type); XmlMembersMapping [] partsMapping = new XmlMembersMapping [1]; partsMapping [0] = xmlImporter.ImportMembersMapping (desc.WrapperName, desc.WrapperNamespace, members, true); bodySerializers [desc] = XmlSerializer.FromMappings (partsMapping) [0]; return bodySerializers [desc]; }
private XmlMembersMapping LoadBodyMapping(MessageDescription message, string mappingKey, out MessagePartDescriptionCollection rpcEncodedTypedMessageBodyParts) { MessagePartDescription returnPart; string wrapperName, wrapperNs; MessagePartDescriptionCollection bodyParts; rpcEncodedTypedMessageBodyParts = null; returnPart = OperationFormatter.IsValidReturnValue(message.Body.ReturnValue) ? message.Body.ReturnValue : null; bodyParts = message.Body.Parts; wrapperName = message.Body.WrapperName; wrapperNs = message.Body.WrapperNamespace; bool isWrapped = (wrapperName != null); bool hasReturnValue = returnPart != null; int paramCount = bodyParts.Count + (hasReturnValue ? 1 : 0); if (paramCount == 0 && !isWrapped) // no need to create serializer { return null; } XmlReflectionMember[] members = new XmlReflectionMember[paramCount]; int paramIndex = 0; if (hasReturnValue) members[paramIndex++] = XmlSerializerHelper.GetXmlReflectionMember(returnPart, IsRpc, isWrapped); for (int i = 0; i < bodyParts.Count; i++) members[paramIndex++] = XmlSerializerHelper.GetXmlReflectionMember(bodyParts[i], IsRpc, isWrapped); if (!isWrapped) wrapperNs = ContractNamespace; return ImportMembersMapping(wrapperName, wrapperNs, members, isWrapped, IsRpc, mappingKey); }
/// <include file='doc\SoapReflectionImporter.uex' path='docs/doc[@for="SoapReflectionImporter.ImportMembersMapping1"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public XmlMembersMapping ImportMembersMapping(string elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool writeAccessors) { return ImportMembersMapping(elementName, ns, members, hasWrapperElement, writeAccessors, false); }
private XmlMembersMapping LoadHeadersMapping(MessageDescription message, string mappingKey) { int headerCount = message.Headers.Count; if (headerCount == 0) return null; int unknownHeaderCount = 0, headerIndex = 0; XmlReflectionMember[] members = new XmlReflectionMember[headerCount]; for (int i = 0; i < headerCount; i++) { MessageHeaderDescription header = message.Headers[i]; if (!header.IsUnknownHeaderCollection) { members[headerIndex++] = XmlSerializerHelper.GetXmlReflectionMember(header, false/*isRpc*/, false/*isWrapped*/); } else { unknownHeaderCount++; } } if (unknownHeaderCount == headerCount) { return null; } if (unknownHeaderCount > 0) { XmlReflectionMember[] newMembers = new XmlReflectionMember[headerCount - unknownHeaderCount]; Array.Copy(members, newMembers, newMembers.Length); members = newMembers; } return ImportMembersMapping(ContractName, ContractNamespace, members, false /*isWrapped*/, false /*isRpc*/, mappingKey); }
/// <include file='doc\SoapReflectionImporter.uex' path='docs/doc[@for="SoapReflectionImporter.ImportMembersMapping3"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public XmlMembersMapping ImportMembersMapping(string elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool writeAccessors, bool validate, XmlMappingAccess access) { ElementAccessor element = new ElementAccessor(); element.IsSoap = true; element.Name = elementName == null || elementName.Length == 0 ? elementName : XmlConvert.EncodeLocalName(elementName); element.Mapping = ImportMembersMapping(members, ns, hasWrapperElement, writeAccessors, validate); element.Mapping.TypeName = elementName; element.Namespace = element.Mapping.Namespace == null ? ns : element.Mapping.Namespace; element.Form = XmlSchemaForm.Qualified; XmlMembersMapping xmlMapping = new XmlMembersMapping(typeScope, element, access); xmlMapping.IsSoap = true; xmlMapping.GenerateSerializer = true; return xmlMapping; }
internal XmlMembersMapping ImportMembersMapping(string elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool rpc, string mappingKey) { string key = mappingKey.StartsWith(":", StringComparison.Ordinal) ? _keyBase + mappingKey : mappingKey; return _parent._importer.ImportMembersMapping(new XmlName(elementName, true /*isEncoded*/), ns, members, hasWrapperElement, rpc, key); }
MemberMapping ImportMemberMapping(XmlReflectionMember xmlReflectionMember, string ns, XmlReflectionMember[] xmlReflectionMembers, XmlSchemaForm form) { SoapAttributes a = xmlReflectionMember.SoapAttributes; if (a.SoapIgnore) return null; MemberMapping member = new MemberMapping(); member.IsSoap = true; member.Name = xmlReflectionMember.MemberName; bool checkSpecified = XmlReflectionImporter.FindSpecifiedMember(xmlReflectionMember.MemberName, xmlReflectionMembers) != null; FieldModel model = new FieldModel(xmlReflectionMember.MemberName, xmlReflectionMember.MemberType, typeScope.GetTypeDesc(xmlReflectionMember.MemberType), checkSpecified, false); member.CheckShouldPersist = model.CheckShouldPersist; member.CheckSpecified = model.CheckSpecified; member.ReadOnly = model.ReadOnly; // || !model.FieldTypeDesc.HasDefaultConstructor; ImportAccessorMapping(member, model, a, ns, form); if (xmlReflectionMember.OverrideIsNullable) member.Elements[0].IsNullable = false; return member; }
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 (atts.SoapElement != null) { throw new Exception("SoapAttributeAttribute and SoapElementAttribute cannot be applied to the same member"); } XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute(); if (atts.SoapAttribute.AttributeName == null) { mapAttribute.AttributeName = rmember.MemberName; } else { mapAttribute.AttributeName = 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; } else { if (typeData.SchemaType == SchemaTypes.Array) { mapMember = new XmlTypeMapMemberList(); } else { mapMember = new XmlTypeMapMemberElement(); } if (atts.SoapElement != null && atts.SoapElement.DataType != null) { 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 = (atts.SoapElement != null && atts.SoapElement.ElementName != null) ? 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); }