public void Add (Type type, string member, SoapAttributes attributes) { if(overrides[GetKey(type, member)] != null) throw new Exception("The attributes for the given type and Member already exist in the collection"); overrides.Add(GetKey(type,member), attributes); }
public void SoapType () { SoapAttributes atts = new SoapAttributes (typeof (TestDefault)); Assert.IsNotNull (atts.SoapType, "#1"); Assert.AreEqual ("testDefault", atts.SoapType.TypeName, "#2"); Assert.AreEqual ("urn:myNS", atts.SoapType.Namespace, "#3"); }
public void Defaults () { SoapAttributes atts = new SoapAttributes (); Assert.IsNull (atts.SoapAttribute, "#1"); Assert.IsNull (atts.SoapDefaultValue, "#2"); Assert.IsNull (atts.SoapElement, "#4"); Assert.IsNull (atts.SoapEnum, "#5"); Assert.AreEqual (false, atts.SoapIgnore, "#6"); Assert.IsNull (atts.SoapType, "#7"); }
/// <include file='doc\SoapAttributeOverrides.uex' path='docs/doc[@for="SoapAttributeOverrides.Add1"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public void Add(Type type, string member, SoapAttributes attributes) { Hashtable members = (Hashtable)types[type]; if (members == null) { members = new Hashtable(); types.Add(type, members); } else if (members[member] != null) { throw new InvalidOperationException(Res.GetString(Res.XmlMultipleAttributeOverrides, type.FullName, member)); } members.Add(member, attributes); }
public void Add(Type type, string member, SoapAttributes attributes) { Hashtable hashtable = (Hashtable) this.types[type]; if (hashtable == null) { hashtable = new Hashtable(); this.types.Add(type, hashtable); } else if (hashtable[member] != null) { throw new InvalidOperationException(Res.GetString("XmlMultipleAttributeOverrides", new object[] { type.FullName, member })); } hashtable.Add(member, attributes); }
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); }
private MemberMapping ImportFieldMapping(FieldModel model, SoapAttributes a, string ns, RecursionLimiter limiter) { if (a.SoapIgnore) { return(null); } MemberMapping member = new MemberMapping(); member.IsSoap = true; member.Name = model.Name; member.CheckShouldPersist = model.CheckShouldPersist; member.CheckSpecified = model.CheckSpecified; member.MemberInfo = model.MemberInfo; member.CheckSpecifiedMemberInfo = model.CheckSpecifiedMemberInfo; member.CheckShouldPersistMethodInfo = model.CheckShouldPersistMethodInfo; member.ReadOnly = model.ReadOnly; // || !model.FieldTypeDesc.HasDefaultConstructor; ImportAccessorMapping(member, model, a, ns, XmlSchemaForm.Unqualified, limiter); return(member); }
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.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(SR.Format(SR.XmlNoSerializableMembers, model.TypeDesc.FullName)); } mapping.Constants = (ConstantMapping[])constants.ToArray(typeof(ConstantMapping)); } return(mapping); }
private ConstantMapping ImportConstantMapping(ConstantModel model) { SoapAttributes attributes = this.GetAttributes(model.FieldInfo); if (attributes.SoapIgnore) { return(null); } if ((attributes.SoapFlags & ~SoapAttributeFlags.Enum) != ((SoapAttributeFlags)0)) { throw new InvalidOperationException(Res.GetString("XmlInvalidEnumAttribute")); } if (attributes.SoapEnum == null) { attributes.SoapEnum = new SoapEnumAttribute(); } return(new ConstantMapping { XmlName = (attributes.SoapEnum.Name.Length == 0) ? model.Name : attributes.SoapEnum.Name, Name = model.Name, Value = model.Value }); }
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); }
private EnumMapping ImportEnumMapping(EnumModel model) { SoapAttributes a = this.GetAttributes(model.Type); string defaultNs = this.defaultNs; if ((a.SoapType != null) && (a.SoapType.Namespace != null)) { defaultNs = a.SoapType.Namespace; } string typeName = XmlConvert.EncodeLocalName(this.XsdTypeName(model.Type, a, model.TypeDesc.Name)); EnumMapping typeMapping = (EnumMapping)this.GetTypeMapping(typeName, defaultNs, model.TypeDesc); if (typeMapping == null) { typeMapping = new EnumMapping { IsSoap = true, TypeDesc = model.TypeDesc, TypeName = typeName, Namespace = defaultNs, IsFlags = model.Type.IsDefined(typeof(FlagsAttribute), false) }; this.typeScope.AddTypeMapping(typeMapping); this.types.Add(typeName, defaultNs, typeMapping); ArrayList list = new ArrayList(); for (int i = 0; i < model.Constants.Length; i++) { ConstantMapping mapping2 = this.ImportConstantMapping(model.Constants[i]); if (mapping2 != null) { list.Add(mapping2); } } if (list.Count == 0) { throw new InvalidOperationException(Res.GetString("XmlNoSerializableMembers", new object[] { model.TypeDesc.FullName })); } typeMapping.Constants = (ConstantMapping[])list.ToArray(typeof(ConstantMapping)); } return(typeMapping); }
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); }
private string GetTypeNamespace(TypeData typeData, string defaultNamespace) { string text = defaultNamespace; SoapAttributes soapAttributes = null; if (!typeData.IsListType && this.attributeOverrides != null) { soapAttributes = this.attributeOverrides[typeData.Type]; } if (soapAttributes == null) { soapAttributes = new SoapAttributes(typeData.Type); } if (soapAttributes.SoapType != null && soapAttributes.SoapType.Namespace != null && soapAttributes.SoapType.Namespace != string.Empty) { text = soapAttributes.SoapType.Namespace; } if (text == null) { return(string.Empty); } return(text); }
string GetTypeNamespace(TypeData typeData, string defaultNamespace) { string membersNamespace = defaultNamespace; SoapAttributes atts = null; if (!typeData.IsListType) { if (attributeOverrides != null) { atts = attributeOverrides[typeData.Type]; } } if (atts == null) { atts = new SoapAttributes(typeData.Type); } if (atts.SoapType != null) { if (atts.SoapType.Namespace != null && atts.SoapType.Namespace != string.Empty) { membersNamespace = atts.SoapType.Namespace; } } if (membersNamespace == null) { return(""); } else { return(membersNamespace); } }
private ConstantMapping ImportConstantMapping(ConstantModel model) { SoapAttributes a = GetAttributes(model.FieldInfo); if (a.SoapIgnore) { return(null); } if ((a.GetSoapFlags() & ~SoapAttributeFlags.Enum) != 0) { throw new InvalidOperationException(SR.XmlInvalidEnumAttribute); } if (a.SoapEnum == null) { a.SoapEnum = new SoapEnumAttribute(); } ConstantMapping constant = new ConstantMapping(); constant.XmlName = a.SoapEnum.Name.Length == 0 ? model.Name : a.SoapEnum.Name; constant.Name = model.Name; constant.Value = model.Value; return(constant); }
[Category ("NotWorking")] // SerializationCodeGenerator outputs wrong xsi:type for flagencoded in #C1 public void TestSerializeDefaultValueAttribute_Encoded () { SoapAttributeOverrides overrides = new SoapAttributeOverrides (); SoapAttributes attr = new SoapAttributes (); attr.SoapAttribute = new SoapAttributeAttribute (); string defaultValueInstance = "nothing"; attr.SoapDefaultValue = defaultValueInstance; overrides.Add (typeof (SimpleClass), "something", attr); // use the default SimpleClass simple = new SimpleClass (); SerializeEncoded (simple, overrides); Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A1"); // same value as default simple.something = defaultValueInstance; SerializeEncoded (simple, overrides); Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A2"); // some other value simple.something = "hello"; SerializeEncoded (simple, overrides); Assert.AreEqual (Infoset ("<SimpleClass id='id1' something='hello' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A3"); attr.SoapAttribute = null; attr.SoapElement = new SoapElementAttribute (); // use the default simple = new SimpleClass (); SerializeEncoded (simple, overrides); Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#B1"); // same value as default simple.something = defaultValueInstance; SerializeEncoded (simple, overrides); Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something xsi:type='xsd:string'>nothing</something></SimpleClass>"), WriterText, "#B2"); // some other value simple.something = "hello"; SerializeEncoded (simple, overrides); Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something xsi:type='xsd:string'>hello</something></SimpleClass>"), WriterText, "#B3"); overrides = new SoapAttributeOverrides (); attr = new SoapAttributes (); attr.SoapElement = new SoapElementAttribute ("flagenc"); overrides.Add (typeof (TestDefault), "flagencoded", attr); // use the default (from MS KB325691) TestDefault testDefault = new TestDefault (); SerializeEncoded (testDefault); Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture, "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" + " <strDefault xsi:type='xsd:string'>Default Value</strDefault>" + " <boolT xsi:type='xsd:boolean'>true</boolT>" + " <boolF xsi:type='xsd:boolean'>false</boolF>" + " <decimalval xsi:type='xsd:decimal'>10</decimalval>" + " <flag xsi:type='FlagEnum'>e1 e4</flag>" + " <flagencoded xsi:type='flagenum'>one four</flagencoded>" + "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#C1"); SerializeEncoded (testDefault, overrides); Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture, "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" + " <strDefault xsi:type='xsd:string'>Default Value</strDefault>" + " <boolT xsi:type='xsd:boolean'>true</boolT>" + " <boolF xsi:type='xsd:boolean'>false</boolF>" + " <decimalval xsi:type='xsd:decimal'>10</decimalval>" + " <flag xsi:type='FlagEnum'>e1 e4</flag>" + " <flagenc xsi:type='flagenum'>one four</flagenc>" + "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#C2"); SerializeEncoded (testDefault, overrides, AnotherNamespace); Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture, "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" + " <strDefault xsi:type='xsd:string'>Default Value</strDefault>" + " <boolT xsi:type='xsd:boolean'>true</boolT>" + " <boolF xsi:type='xsd:boolean'>false</boolF>" + " <decimalval xsi:type='xsd:decimal'>10</decimalval>" + " <flag xmlns:q2='{2}' xsi:type='q2:FlagEnum'>e1 e4</flag>" + " <flagenc xmlns:q3='{2}' xsi:type='q3:flagenum'>one four</flagenc>" + "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace, AnotherNamespace)), WriterText, "#C3"); // non-default values testDefault.strDefault = "Some Text"; testDefault.boolT = false; testDefault.boolF = true; testDefault.decimalval = 20m; testDefault.flag = FlagEnum.e2; testDefault.flagencoded = FlagEnum_Encoded.e2 | FlagEnum_Encoded.e1; SerializeEncoded (testDefault); Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture, "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" + " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" + " <boolT xsi:type='xsd:boolean'>false</boolT>" + " <boolF xsi:type='xsd:boolean'>true</boolF>" + " <decimalval xsi:type='xsd:decimal'>20</decimalval>" + " <flag xsi:type='FlagEnum'>e2</flag>" + " <flagencoded xsi:type='flagenum'>one two</flagencoded>" + "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#C4"); SerializeEncoded (testDefault, overrides); Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture, "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" + " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" + " <boolT xsi:type='xsd:boolean'>false</boolT>" + " <boolF xsi:type='xsd:boolean'>true</boolF>" + " <decimalval xsi:type='xsd:decimal'>20</decimalval>" + " <flag xsi:type='FlagEnum'>e2</flag>" + " <flagenc xsi:type='flagenum'>one two</flagenc>" + "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#C5"); attr = new SoapAttributes (); attr.SoapType = new SoapTypeAttribute ("flagenum", "yetanother:urn"); overrides.Add (typeof (FlagEnum_Encoded), attr); SerializeEncoded (testDefault, overrides, AnotherNamespace); Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture, "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" + " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" + " <boolT xsi:type='xsd:boolean'>false</boolT>" + " <boolF xsi:type='xsd:boolean'>true</boolF>" + " <decimalval xsi:type='xsd:decimal'>20</decimalval>" + " <flag xmlns:q2='{2}' xsi:type='q2:FlagEnum'>e2</flag>" + " <flagenc xmlns:q3='yetanother:urn' xsi:type='q3:flagenum'>one two</flagenc>" + "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace, AnotherNamespace)), WriterText, "#C6"); attr = new SoapAttributes (); attr.SoapType = new SoapTypeAttribute ("testDefault"); overrides.Add (typeof (TestDefault), attr); SerializeEncoded (testDefault, overrides, AnotherNamespace); Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture, "<q1:testDefault id='id1' xmlns:q1='{2}' xmlns:xsd='{0}' xmlns:xsi='{1}'>" + " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" + " <boolT xsi:type='xsd:boolean'>false</boolT>" + " <boolF xsi:type='xsd:boolean'>true</boolF>" + " <decimalval xsi:type='xsd:decimal'>20</decimalval>" + " <flag xsi:type='q1:FlagEnum'>e2</flag>" + " <flagenc xmlns:q2='yetanother:urn' xsi:type='q2:flagenum'>one two</flagenc>" + "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace, AnotherNamespace)), WriterText, "#C7"); }
/// <summary>Adds a <see cref="T:System.Xml.Serialization.SoapAttributes" /> to a collection of <see cref="T:System.Xml.Serialization.SoapAttributes" /> objects. The <paramref name="type" /> parameter specifies an object to be overridden by the <see cref="T:System.Xml.Serialization.SoapAttributes" />.</summary> /// <param name="type">The <see cref="T:System.Type" /> of the object that is overridden. </param> /// <param name="attributes">A <see cref="T:System.Xml.Serialization.SoapAttributes" /> that represents the overriding attributes. </param> public void Add(Type type, SoapAttributes attributes) { this.Add(type, string.Empty, attributes); }
private TypeMapping ImportTypeMapping(TypeModel model, string dataType, RecursionLimiter limiter) { if (dataType.Length > 0) { if (!model.TypeDesc.IsPrimitive) { throw new InvalidOperationException(Res.GetString("XmlInvalidDataTypeUsage", new object[] { dataType, "SoapElementAttribute.DataType" })); } TypeDesc typeDesc = this.typeScope.GetTypeDesc(dataType, "http://www.w3.org/2001/XMLSchema"); if (typeDesc == null) { throw new InvalidOperationException(Res.GetString("XmlInvalidXsdDataType", new object[] { dataType, "SoapElementAttribute.DataType", new XmlQualifiedName(dataType, "http://www.w3.org/2001/XMLSchema").ToString() })); } if (model.TypeDesc.FullName != typeDesc.FullName) { throw new InvalidOperationException(Res.GetString("XmlDataTypeMismatch", new object[] { dataType, "SoapElementAttribute.DataType", model.TypeDesc.FullName })); } } if ((this.GetAttributes(model.Type).SoapFlags & ~SoapAttributeFlags.Type) != ((SoapAttributeFlags)0)) { throw new InvalidOperationException(Res.GetString("XmlInvalidTypeAttributes", new object[] { model.Type.FullName })); } switch (model.TypeDesc.Kind) { case TypeKind.Root: case TypeKind.Struct: case TypeKind.Class: { if (!model.TypeDesc.IsOptionalValue) { return(this.ImportStructLikeMapping((StructModel)model, limiter)); } TypeDesc baseTypeDesc = model.TypeDesc.BaseTypeDesc; SoapAttributes attributes = this.GetAttributes(baseTypeDesc.Type); string defaultNs = this.defaultNs; if ((attributes.SoapType != null) && (attributes.SoapType.Namespace != null)) { defaultNs = attributes.SoapType.Namespace; } TypeDesc desc3 = string.IsNullOrEmpty(dataType) ? model.TypeDesc.BaseTypeDesc : this.typeScope.GetTypeDesc(dataType, "http://www.w3.org/2001/XMLSchema"); string typeName = string.IsNullOrEmpty(dataType) ? model.TypeDesc.BaseTypeDesc.Name : dataType; TypeMapping baseMapping = this.GetTypeMapping(typeName, defaultNs, desc3); if (baseMapping == null) { baseMapping = this.ImportTypeMapping(this.modelScope.GetTypeModel(baseTypeDesc.Type), dataType, limiter); } return(this.CreateNullableMapping(baseMapping, model.TypeDesc.Type)); } case TypeKind.Primitive: return(this.ImportPrimitiveMapping((PrimitiveModel)model, dataType)); case TypeKind.Enum: return(this.ImportEnumMapping((EnumModel)model)); case TypeKind.Array: case TypeKind.Collection: case TypeKind.Enumerable: return(this.ImportArrayLikeMapping((ArrayModel)model, limiter)); } throw new NotSupportedException(Res.GetString("XmlUnsupportedSoapTypeKind", new object[] { model.TypeDesc.FullName })); }
public void TypeMapping_InvalidDefault () { SoapAttributes attrs = new SoapAttributes (typeof (Field_Encoded).GetMember ("Modifiers")[0]); attrs.SoapDefaultValue = 2; // not a defined enum value SoapAttributeOverrides overrides = new SoapAttributeOverrides (); overrides.Add (typeof (Field_Encoded), "Modifiers", attrs); try { Map (typeof (Field_Encoded), overrides); Assert.Fail ("#A1"); } catch (InvalidOperationException ex) { // Enum System.Int32 cannot be converted to MonoTests.System.Xml.TestClasses.MapModifiers Assert.IsNotNull (ex.Message, "#A2"); Assert.IsTrue (ex.Message.IndexOf (typeof (int).FullName) != -1, "#A3"); Assert.IsTrue (ex.Message.IndexOf (typeof (MapModifiers).FullName) != -1, "#A4"); Assert.IsNull (ex.InnerException, "#A5"); } attrs.SoapDefaultValue = "2"; // not of the same type as the underlying enum type (System.Int32) try { Map (typeof (Field_Encoded), overrides); Assert.Fail ("#B1"); } catch (InvalidOperationException ex) { // Enum System.String cannot be converted to MonoTests.System.Xml.TestClasses.MapModifiers Assert.IsNotNull (ex.Message, "#B2"); Assert.IsTrue (ex.Message.IndexOf (typeof (string).FullName) != -1, "#B3"); Assert.IsTrue (ex.Message.IndexOf (typeof (MapModifiers).FullName) != -1, "#B4"); Assert.IsNull (ex.InnerException, "#B5"); } attrs.SoapDefaultValue = EnumDefaultValueNF.e2; // other enum type try { Map (typeof (Field_Encoded), overrides); Assert.Fail ("#C1"); } catch (InvalidOperationException ex) { // Enum MonoTests.System.Xml.TestClasses.EnumDefaultValueNF cannot be converted to MonoTests.System.Xml.TestClasses.MapModifiers Assert.IsNotNull (ex.Message, "#C2"); Assert.IsTrue (ex.Message.IndexOf (typeof (EnumDefaultValueNF).FullName) != -1, "#C3"); Assert.IsTrue (ex.Message.IndexOf (typeof (MapModifiers).FullName) != -1, "#C4"); Assert.IsNull (ex.InnerException, "#C5"); } attrs.SoapDefaultValue = (MapModifiers) 20; // non-existing enum value try { Map (typeof (Field_Encoded), overrides); Assert.Fail ("#D1"); } catch (InvalidOperationException ex) { // Value '20' cannot be converted to MonoTests.System.Xml.TestClasses.MapModifiers Assert.IsNotNull (ex.Message, "#D2"); Assert.IsTrue (ex.Message.IndexOf ("'20'") != -1, "#D3"); Assert.IsTrue (ex.Message.IndexOf (typeof (MapModifiers).FullName) != -1, "#D4"); Assert.IsNull (ex.InnerException, "#D5"); } }
/// <include file='doc\SoapAttributeOverrides.uex' path='docs/doc[@for="SoapAttributeOverrides.Add"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public void Add(Type type, SoapAttributes attributes) { Add(type, string.Empty, attributes); }
private bool InitializeStructMembers(StructMapping mapping, StructModel model, RecursionLimiter limiter) { if (mapping.IsFullyInitialized) { return(true); } if (model.TypeDesc.BaseTypeDesc != null) { StructMapping baseMapping = ImportStructLikeMapping((StructModel)_modelScope.GetTypeModel(model.Type.BaseType, false), limiter); // check to see if the import of the baseMapping was deferred int baseIndex = limiter.DeferredWorkItems.IndexOf(mapping.BaseMapping); if (baseIndex < 0) { mapping.BaseMapping = baseMapping; } else { // the import of the baseMapping was deferred, make sure that the derived mappings is deferred as well if (!limiter.DeferredWorkItems.Contains(mapping)) { limiter.DeferredWorkItems.Add(new ImportStructWorkItem(model, mapping)); } // make sure that baseMapping get processed before the derived int top = limiter.DeferredWorkItems.Count - 1; if (baseIndex < top) { ImportStructWorkItem baseMappingWorkItem = limiter.DeferredWorkItems[baseIndex]; limiter.DeferredWorkItems[baseIndex] = limiter.DeferredWorkItems[top]; limiter.DeferredWorkItems[top] = baseMappingWorkItem; } return(false); } } ArrayList members = new ArrayList(); foreach (MemberInfo memberInfo in model.GetMemberInfos()) { if (!(memberInfo is FieldInfo) && !(memberInfo is PropertyInfo)) { continue; } SoapAttributes memberAttrs = GetAttributes(memberInfo); if (memberAttrs.SoapIgnore) { continue; } FieldModel fieldModel = model.GetFieldModel(memberInfo); if (fieldModel == null) { continue; } MemberMapping member = ImportFieldMapping(fieldModel, memberAttrs, mapping.Namespace, limiter); if (member == null) { continue; } if (!member.TypeDesc.IsPrimitive && !member.TypeDesc.IsEnum && !member.TypeDesc.IsOptionalValue) { if (model.TypeDesc.IsValueType) { throw new NotSupportedException(SR.Format(SR.XmlRpcRefsInValueType, model.TypeDesc.FullName)); } if (member.TypeDesc.IsValueType) { throw new NotSupportedException(SR.Format(SR.XmlRpcNestedValueType, member.TypeDesc.FullName)); } } if (mapping.BaseMapping != null) { if (mapping.BaseMapping.Declares(member, mapping.TypeName)) { continue; } } members.Add(member); } mapping.Members = (MemberMapping[])members.ToArray(typeof(MemberMapping)); if (mapping.BaseMapping == null) { mapping.BaseMapping = GetRootMapping(); } IncludeTypes(model.Type, limiter); return(true); }
[Category ("NotWorking")] // mark it NotWorking until fixes have landed in svn /* #if NET_2_0 [Category ("NotWorking")] // minOccurs is 1 on Mono #endif */ public void ExportClass_SimpleClass_Array () { SoapAttributeOverrides overrides = new SoapAttributeOverrides (); SoapAttributes attr = new SoapAttributes (); SoapElementAttribute element = new SoapElementAttribute (); element.ElementName = "saying"; element.IsNullable = true; attr.SoapElement = element; overrides.Add (typeof (SimpleClass), "something", attr); SoapReflectionImporter ri = new SoapReflectionImporter (overrides, "NSSimpleClassArray"); XmlSchemas schemas = new XmlSchemas (); SoapSchemaExporter sx = new SoapSchemaExporter (schemas); XmlTypeMapping tm = ri.ImportTypeMapping (typeof (SimpleClass[])); sx.ExportTypeMapping (tm); Assert.AreEqual (1, schemas.Count, "#1"); StringWriter sw = new StringWriter (); schemas[0].Write (sw); Assert.AreEqual (string.Format (CultureInfo.InvariantCulture, "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" + #if NET_2_0 "<xs:schema xmlns:tns=\"NSSimpleClassArray\" elementFormDefault=\"qualified\" targetNamespace=\"NSSimpleClassArray\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" + #else "<xs:schema xmlns:tns=\"NSSimpleClassArray\" targetNamespace=\"NSSimpleClassArray\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" + #endif " <xs:import namespace=\"http://schemas.xmlsoap.org/soap/encoding/\" />{0}" + " <xs:import namespace=\"http://schemas.xmlsoap.org/wsdl/\" />{0}" + " <xs:complexType name=\"ArrayOfSimpleClass\">{0}" + " <xs:complexContent mixed=\"false\">{0}" + " <xs:restriction xmlns:q1=\"http://schemas.xmlsoap.org/soap/encoding/\" base=\"q1:Array\">{0}" + " <xs:attribute d5p1:arrayType=\"tns:SimpleClass[]\" ref=\"q1:arrayType\" xmlns:d5p1=\"http://schemas.xmlsoap.org/wsdl/\" />{0}" + " </xs:restriction>{0}" + " </xs:complexContent>{0}" + " </xs:complexType>{0}" + " <xs:complexType name=\"SimpleClass\">{0}" + " <xs:sequence>{0}" + #if NET_2_0 " <xs:element minOccurs=\"0\" maxOccurs=\"1\" form=\"unqualified\" name=\"saying\" nillable=\"true\" type=\"xs:string\" />{0}" + #else " <xs:element minOccurs=\"1\" maxOccurs=\"1\" name=\"saying\" type=\"xs:string\" />{0}" + #endif " </xs:sequence>{0}" + " </xs:complexType>{0}" + "</xs:schema>", Environment.NewLine), sw.ToString (), "#2"); }
[Category ("NotWorking")] // mark it NotWorking until fixes have landed in svn /* #if NET_2_0 [Category ("NotWorking")] // minOccurs is 1 on Mono #endif */ public void ExportClass_SimpleClass () { SoapAttributeOverrides overrides = new SoapAttributeOverrides (); SoapAttributes attr = new SoapAttributes (); SoapElementAttribute element = new SoapElementAttribute (); element.ElementName = "saying"; element.IsNullable = true; attr.SoapElement = element; overrides.Add (typeof (SimpleClass), "something", attr); XmlSchemas schemas = Export (typeof (SimpleClass), overrides, "NSSimpleClass"); Assert.AreEqual (1, schemas.Count, "#1"); StringWriter sw = new StringWriter (); schemas[0].Write (sw); Assert.AreEqual (string.Format (CultureInfo.InvariantCulture, "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" + #if NET_2_0 "<xs:schema xmlns:tns=\"NSSimpleClass\" elementFormDefault=\"qualified\" targetNamespace=\"NSSimpleClass\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" + #else "<xs:schema xmlns:tns=\"NSSimpleClass\" targetNamespace=\"NSSimpleClass\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" + #endif " <xs:complexType name=\"SimpleClass\">{0}" + " <xs:sequence>{0}" + #if NET_2_0 " <xs:element minOccurs=\"0\" maxOccurs=\"1\" form=\"unqualified\" name=\"saying\" nillable=\"true\" type=\"xs:string\" />{0}" + #else " <xs:element minOccurs=\"1\" maxOccurs=\"1\" name=\"saying\" type=\"xs:string\" />{0}" + #endif " </xs:sequence>{0}" + " </xs:complexType>{0}" + "</xs:schema>", Environment.NewLine), sw.ToString (), "#2"); }
MemberMapping ImportFieldMapping(FieldModel model, SoapAttributes a, string ns) { if (a.SoapIgnore) return null; MemberMapping member = new MemberMapping(); member.IsSoap = true; member.Name = model.Name; member.CheckShouldPersist = model.CheckShouldPersist; member.CheckSpecified = model.CheckSpecified; member.ReadOnly = model.ReadOnly; // || !model.FieldTypeDesc.HasDefaultConstructor; ImportAccessorMapping(member, model, a, ns, XmlSchemaForm.Unqualified); return member; }
object GetDefaultValue(TypeDesc fieldTypeDesc, SoapAttributes a) { if (a.SoapDefaultValue == null || a.SoapDefaultValue == DBNull.Value) return null; if (!(fieldTypeDesc.Kind == TypeKind.Primitive || fieldTypeDesc.Kind == TypeKind.Enum)) { a.SoapDefaultValue = null; return a.SoapDefaultValue; } // for enums validate and return a string representation if (fieldTypeDesc.Kind == TypeKind.Enum) { if (fieldTypeDesc != typeScope.GetTypeDesc(a.SoapDefaultValue.GetType())) throw new InvalidOperationException(Res.GetString(Res.XmlInvalidDefaultEnumValue, a.SoapDefaultValue.GetType().FullName, fieldTypeDesc.FullName)); string strValue = Enum.Format(a.SoapDefaultValue.GetType(), a.SoapDefaultValue, "G").Replace(",", " "); string numValue = Enum.Format(a.SoapDefaultValue.GetType(), a.SoapDefaultValue, "D"); if (strValue == numValue) // means enum value wasn't recognized throw new InvalidOperationException(Res.GetString(Res.XmlInvalidDefaultValue, strValue, a.SoapDefaultValue.GetType().FullName)); return strValue; } return a.SoapDefaultValue; }
string GetTypeNamespace (TypeData typeData, string defaultNamespace) { string membersNamespace = defaultNamespace; SoapAttributes atts = null; if (!typeData.IsListType) { if (attributeOverrides != null) atts = attributeOverrides[typeData.Type]; } if (atts == null) atts = new SoapAttributes (typeData.Type); if (atts.SoapType != null) { if (atts.SoapType.Namespace != null && atts.SoapType.Namespace != string.Empty) membersNamespace = atts.SoapType.Namespace; } if (membersNamespace == null) return ""; else return membersNamespace; }
XmlTypeMapping CreateTypeMapping (TypeData typeData, string defaultXmlType, string defaultNamespace) { string membersNamespace = defaultNamespace; bool includeInSchema = true; SoapAttributes atts = null; if (defaultXmlType == null) defaultXmlType = typeData.XmlType; if (!typeData.IsListType) { if (attributeOverrides != null) atts = attributeOverrides[typeData.Type]; if (atts != null && typeData.SchemaType == SchemaTypes.Primitive) throw new InvalidOperationException ("SoapType attribute may not be specified for the type " + typeData.FullTypeName); } if (atts == null) atts = new SoapAttributes (typeData.Type); if (atts.SoapType != null) { if (atts.SoapType.Namespace != null && atts.SoapType.Namespace != string.Empty) membersNamespace = atts.SoapType.Namespace; if (atts.SoapType.TypeName != null && atts.SoapType.TypeName != string.Empty) defaultXmlType = XmlConvert.EncodeLocalName (atts.SoapType.TypeName); includeInSchema = atts.SoapType.IncludeInSchema; } if (membersNamespace == null) membersNamespace = ""; XmlTypeMapping map = new XmlTypeMapping (defaultXmlType, membersNamespace, typeData, defaultXmlType, membersNamespace); map.IncludeInSchema = includeInSchema; relatedMaps.Add (map); return map; }
public void TestImportMembersMapping() { Type type = typeof (SimpleClass); SoapAttributes attrs = new SoapAttributes (); SoapAttributeOverrides overrides = new SoapAttributeOverrides (); overrides.Add (typeof (SimpleClass), attrs); XmlReflectionMember[] members = new XmlReflectionMember[0]; XmlMembersMapping mm; try { mm = MembersMap(type, overrides, members, true, true); Assert.Fail("Should not be able to fetch an empty XmlMembersMapping"); } catch (Exception) { } XmlReflectionMember rm = new XmlReflectionMember(); rm.IsReturnValue = false; rm.MemberName = "something"; rm.MemberType = typeof(string); members = new XmlReflectionMember[1]; members[0] = rm; mm = MembersMap(type, overrides, members, false, false); Equals(mm.Count, 1); XmlMemberMapping smm = mm[0]; Assert.IsFalse (smm.Any, "#1"); Assert.AreEqual ("something", smm.ElementName, "#2"); Assert.AreEqual ("something", smm.MemberName, "#3"); Assert.IsNull (smm.Namespace, "#4"); Assert.AreEqual ("System.String", smm.TypeFullName, "#5"); Assert.AreEqual ("string", smm.TypeName, "#6"); Assert.AreEqual (XmlSchemaNamespace, smm.TypeNamespace, "#7"); rm = new XmlReflectionMember(); rm.IsReturnValue = false; rm.MemberName = "nothing"; rm.MemberType = typeof(string); members = new XmlReflectionMember[1]; members[0] = rm; mm = MembersMap(type, overrides, members, false, false); Assert.AreEqual (1 , mm.Count, "#8"); }
private TypeMapping ImportTypeMapping(TypeModel model, string dataType, RecursionLimiter limiter) { if (dataType.Length > 0) { if (!model.TypeDesc.IsPrimitive) { throw new InvalidOperationException(SR.Format(SR.XmlInvalidDataTypeUsage, dataType, "SoapElementAttribute.DataType")); } TypeDesc td = _typeScope.GetTypeDesc(dataType, XmlSchema.Namespace); if (td == null) { throw new InvalidOperationException(SR.Format(SR.XmlInvalidXsdDataType, dataType, "SoapElementAttribute.DataType", new XmlQualifiedName(dataType, XmlSchema.Namespace).ToString())); } if (model.TypeDesc.FullName != td.FullName) { throw new InvalidOperationException(SR.Format(SR.XmlDataTypeMismatch, dataType, "SoapElementAttribute.DataType", model.TypeDesc.FullName)); } } SoapAttributes a = GetAttributes(model.Type); if ((a.GetSoapFlags() & ~SoapAttributeFlags.Type) != 0) { throw new InvalidOperationException(SR.Format(SR.XmlInvalidTypeAttributes, model.Type.FullName)); } switch (model.TypeDesc.Kind) { case TypeKind.Enum: return(ImportEnumMapping((EnumModel)model)); case TypeKind.Primitive: return(ImportPrimitiveMapping((PrimitiveModel)model, dataType)); case TypeKind.Array: case TypeKind.Collection: case TypeKind.Enumerable: return(ImportArrayLikeMapping((ArrayModel)model, limiter)); case TypeKind.Root: case TypeKind.Class: case TypeKind.Struct: if (model.TypeDesc.IsOptionalValue) { TypeDesc baseTypeDesc = model.TypeDesc.BaseTypeDesc; SoapAttributes baseAttributes = GetAttributes(baseTypeDesc.Type); string typeNs = _defaultNs; if (baseAttributes.SoapType != null && baseAttributes.SoapType.Namespace != null) { typeNs = baseAttributes.SoapType.Namespace; } TypeDesc valueTypeDesc = string.IsNullOrEmpty(dataType) ? model.TypeDesc.BaseTypeDesc : _typeScope.GetTypeDesc(dataType, XmlSchema.Namespace); string xsdTypeName = string.IsNullOrEmpty(dataType) ? model.TypeDesc.BaseTypeDesc.Name : dataType; TypeMapping baseMapping = GetTypeMapping(xsdTypeName, typeNs, valueTypeDesc); if (baseMapping == null) { baseMapping = ImportTypeMapping(_modelScope.GetTypeModel(baseTypeDesc.Type), dataType, limiter); } return(CreateNullableMapping(baseMapping, model.TypeDesc.Type)); } else { return(ImportStructLikeMapping((StructModel)model, limiter)); } default: throw new NotSupportedException(SR.Format(SR.XmlUnsupportedSoapTypeKind, model.TypeDesc.FullName)); } }
public void SoapDefaultValue () { SoapAttributes atts = new SoapAttributes (typeof (TestDefault).GetMember("strDefault")[0]); Assert.IsNotNull (atts.SoapDefaultValue, "#1"); Assert.AreEqual ("Default Value", atts.SoapDefaultValue, "#2"); }
private StructMapping ImportStructLikeMapping(StructModel model, RecursionLimiter limiter) { if (model.TypeDesc.Kind == TypeKind.Root) { return(GetRootMapping()); } 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); StructMapping mapping = (StructMapping)GetTypeMapping(typeName, typeNs, model.TypeDesc); if (mapping == null) { mapping = new StructMapping(); mapping.IsSoap = true; mapping.TypeDesc = model.TypeDesc; mapping.Namespace = typeNs; mapping.TypeName = typeName; if (a.SoapType != null) { mapping.IncludeInSchema = a.SoapType.IncludeInSchema; } _typeScope.AddTypeMapping(mapping); _types.Add(typeName, typeNs, mapping); if (limiter.IsExceededLimit) { limiter.DeferredWorkItems.Add(new ImportStructWorkItem(model, mapping)); return(mapping); } limiter.Depth++; InitializeStructMembers(mapping, model, limiter); while (limiter.DeferredWorkItems.Count > 0) { int index = limiter.DeferredWorkItems.Count - 1; ImportStructWorkItem item = limiter.DeferredWorkItems[index]; if (InitializeStructMembers(item.Mapping, item.Model, limiter)) { // // if InitializeStructMembers returns true, then there were *no* changes to the DeferredWorkItems // #if DEBUG // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe if (index != limiter.DeferredWorkItems.Count - 1) { throw new InvalidOperationException(SR.Format(SR.XmlInternalErrorDetails, "DeferredWorkItems.Count have changed")); } if (item != limiter.DeferredWorkItems[index]) { throw new InvalidOperationException(SR.Format(SR.XmlInternalErrorDetails, "DeferredWorkItems.Top have changed")); } #endif // Remove the last work item limiter.DeferredWorkItems.RemoveAt(index); } } limiter.Depth--; } return(mapping); }
public void SoapEnum () { SoapAttributes atts = new SoapAttributes (typeof (FlagEnum_Encoded).GetMember("e1")[0]); Assert.IsNotNull (atts.SoapEnum, "#1"); Assert.AreEqual ("one", atts.SoapEnum.Name, "#2"); }
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); }
private bool InitializeStructMembers(StructMapping mapping, StructModel model, RecursionLimiter limiter) { if (!mapping.IsFullyInitialized) { if (model.TypeDesc.BaseTypeDesc != null) { StructMapping mapping2 = this.ImportStructLikeMapping((StructModel)this.modelScope.GetTypeModel(model.Type.BaseType, false), limiter); int index = limiter.DeferredWorkItems.IndexOf(mapping.BaseMapping); if (index >= 0) { if (!limiter.DeferredWorkItems.Contains(mapping)) { limiter.DeferredWorkItems.Add(new ImportStructWorkItem(model, mapping)); } int num2 = limiter.DeferredWorkItems.Count - 1; if (index < num2) { ImportStructWorkItem item = limiter.DeferredWorkItems[index]; limiter.DeferredWorkItems[index] = limiter.DeferredWorkItems[num2]; limiter.DeferredWorkItems[num2] = item; } return(false); } mapping.BaseMapping = mapping2; } ArrayList list = new ArrayList(); foreach (MemberInfo info in model.GetMemberInfos()) { if ((info.MemberType & (MemberTypes.Property | MemberTypes.Field)) != 0) { SoapAttributes a = this.GetAttributes(info); if (!a.SoapIgnore) { FieldModel fieldModel = model.GetFieldModel(info); if (fieldModel != null) { MemberMapping member = this.ImportFieldMapping(fieldModel, a, mapping.Namespace, limiter); if (member != null) { if ((!member.TypeDesc.IsPrimitive && !member.TypeDesc.IsEnum) && !member.TypeDesc.IsOptionalValue) { if (model.TypeDesc.IsValueType) { throw new NotSupportedException(Res.GetString("XmlRpcRefsInValueType", new object[] { model.TypeDesc.FullName })); } if (member.TypeDesc.IsValueType) { throw new NotSupportedException(Res.GetString("XmlRpcNestedValueType", new object[] { member.TypeDesc.FullName })); } } if ((mapping.BaseMapping == null) || !mapping.BaseMapping.Declares(member, mapping.TypeName)) { list.Add(member); } } } } } } mapping.Members = (MemberMapping[])list.ToArray(typeof(MemberMapping)); if (mapping.BaseMapping == null) { mapping.BaseMapping = this.GetRootMapping(); } this.IncludeTypes(model.Type, limiter); } return(true); }
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); }
StructMapping ImportStructLikeMapping(StructModel model) { if (model.TypeDesc.Kind == TypeKind.Root) { return(GetRootMapping()); } SoapAttributes a = GetAttributes(model.Type); string typeNs = defaultNs; if (a.SoapType != null && a.SoapType.Namespace != null) { typeNs = a.SoapType.Namespace; } string typeName = string.Empty; if (a.SoapType != null) { typeName = a.SoapType.TypeName; } if (typeName.Length == 0) { typeName = model.TypeDesc.Name; } StructMapping mapping = (StructMapping)GetTypeMapping(typeName, typeNs, model.TypeDesc); if (mapping == null) { mapping = new StructMapping(); mapping.IsSoap = true; mapping.TypeDesc = model.TypeDesc; mapping.Namespace = typeNs; mapping.TypeName = typeName; if (a.SoapType != null) { mapping.IncludeInSchema = a.SoapType.IncludeInSchema; } typeScope.AddTypeMapping(mapping); types.Add(typeName, typeNs, mapping); if (model.TypeDesc.BaseTypeDesc != null) { mapping.BaseMapping = ImportStructLikeMapping((StructModel)modelScope.GetTypeModel(model.Type.BaseType, false)); } ArrayList members = new ArrayList(); foreach (MemberInfo memberInfo in model.GetMemberInfos()) { SoapAttributes memberAttrs = GetAttributes(memberInfo); if (memberAttrs.SoapIgnore) { continue; } FieldModel fieldModel = model.GetFieldModel(memberInfo); if (fieldModel == null) { continue; } MemberMapping member = ImportFieldMapping(fieldModel, memberAttrs, mapping.Namespace); if (member == null) { continue; } if (!member.TypeDesc.IsPrimitive && !member.TypeDesc.IsEnum) { if (model.TypeDesc.IsValueType) { throw new NotSupportedException(Res.GetString(Res.XmlRpcRefsInValueType, model.TypeDesc.FullName)); } if (member.TypeDesc.IsValueType) { throw new NotSupportedException(Res.GetString(Res.XmlRpcNestedValueType, member.TypeDesc.FullName)); } } if (mapping.BaseMapping != null) { if (mapping.BaseMapping.Declares(member, mapping.TypeName)) { continue; } } members.Add(member); } mapping.Members = (MemberMapping[])members.ToArray(typeof(MemberMapping)); if (mapping.BaseMapping == null) { mapping.BaseMapping = GetRootMapping(); } IncludeTypes(model.Type); } return(mapping); }
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; }
[Category ("NotDotNet")] // wrong error message is reported in .NET 1.1 #endif public void TypeMapping_Attribute_ComplexType () { SoapAttributes attrs = new SoapAttributes (typeof (Field_Encoded).GetMember ("Names")[0]); attrs.SoapAttribute = new SoapAttributeAttribute (); // SoapAttribute cannot be used to encode complex types SoapAttributeOverrides overrides = new SoapAttributeOverrides (); overrides.Add (typeof (Field_Encoded), "Names", attrs); try { Map (typeof (Field_Encoded), overrides); Assert.Fail ("#A1"); } catch (InvalidOperationException ex) { // Cannot serialize member 'Names' of type System.String[]. // SoapAttribute cannot be used to encode complex types. Assert.IsNotNull (ex.Message, "#A2"); Assert.IsTrue (ex.Message.IndexOf (typeof (string[]).FullName) != -1, "#A3"); Assert.IsTrue (ex.Message.IndexOf ("SoapAttribute") != -1, "#A4"); Assert.IsNull (ex.InnerException, "#A5"); } }
void ImportAccessorMapping(MemberMapping accessor, FieldModel model, SoapAttributes a, string ns, XmlSchemaForm form) { Type accessorType = model.FieldType; string accessorName = model.Name; accessor.TypeDesc = typeScope.GetTypeDesc(accessorType); if (accessor.TypeDesc.IsVoid) { throw new InvalidOperationException(Res.GetString(Res.XmlInvalidVoid)); } SoapAttributeFlags flags = a.SoapFlags; if ((flags & SoapAttributeFlags.Attribute) == SoapAttributeFlags.Attribute) { if (!accessor.TypeDesc.IsPrimitive && !accessor.TypeDesc.IsEnum) throw new InvalidOperationException(Res.GetString(Res.XmlIllegalSoapAttribute, accessorName, accessor.TypeDesc.FullName)); if ((flags & SoapAttributeFlags.Attribute) != flags) throw new InvalidOperationException(Res.GetString(Res.XmlInvalidElementAttribute)); AttributeAccessor attribute = new AttributeAccessor(); attribute.Name = Accessor.EscapeQName(a.SoapAttribute == null || a.SoapAttribute.AttributeName.Length == 0 ? accessorName : a.SoapAttribute.AttributeName); attribute.Namespace = a.SoapAttribute == null || a.SoapAttribute.Namespace == null ? ns : a.SoapAttribute.Namespace; attribute.Form = XmlSchemaForm.Qualified; // attributes are always qualified since they're only used for encoded soap headers attribute.Mapping = ImportTypeMapping(modelScope.GetTypeModel(accessorType), (a.SoapAttribute == null ? String.Empty : a.SoapAttribute.DataType)); attribute.Default = GetDefaultValue(model.FieldTypeDesc, a); accessor.Attribute = attribute; accessor.Elements = new ElementAccessor[0]; } else { if ((flags & SoapAttributeFlags.Element) != flags) throw new InvalidOperationException(Res.GetString(Res.XmlInvalidElementAttribute)); ElementAccessor element = new ElementAccessor(); element.IsSoap = true; element.Name = XmlConvert.EncodeLocalName(a.SoapElement == null || a.SoapElement.ElementName.Length == 0 ? accessorName : a.SoapElement.ElementName); element.Namespace = ns; element.Form = form; element.Mapping = ImportTypeMapping(modelScope.GetTypeModel(accessorType), (a.SoapElement == null ? String.Empty : a.SoapElement.DataType)); if (a.SoapElement != null) element.IsNullable = a.SoapElement.IsNullable; accessor.Elements = new ElementAccessor[] { element }; } }
internal XmlReflectionMember(string name, Type type, SoapAttributes attributes) { this.memberName = name; this.memberType = type; this.soapAttributes = attributes; }
internal string XsdTypeName(Type type, SoapAttributes a, string name) { string typeName = name; if (a.SoapType != null && a.SoapType.TypeName.Length > 0) typeName = a.SoapType.TypeName; if (type.IsGenericType && typeName.IndexOf('{') >= 0) { Type genType = type.GetGenericTypeDefinition(); Type[] names = genType.GetGenericArguments(); Type[] types = type.GetGenericArguments(); for (int i = 0; i < names.Length; i++) { string argument = "{" + names[i] + "}"; if (typeName.Contains(argument)) { typeName = typeName.Replace(argument, XsdTypeName(types[i])); if (typeName.IndexOf('{') < 0) { break; } } } } return typeName; }
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); }