internal MemberMapping FindDeclaringMapping(MemberMapping member, out StructMapping declaringMapping, string parent) { declaringMapping = null; if (this.BaseMapping != null) { MemberMapping mapping = this.BaseMapping.FindDeclaringMapping(member, out declaringMapping, parent); if (mapping != null) { return mapping; } } if (this.members != null) { for (int i = 0; i < this.members.Length; i++) { if (this.members[i].Name == member.Name) { if (this.members[i].TypeDesc != member.TypeDesc) { throw new InvalidOperationException(Res.GetString("XmlHiddenMember", new object[] { parent, member.Name, member.TypeDesc.FullName, base.TypeName, this.members[i].Name, this.members[i].TypeDesc.FullName })); } if (!this.members[i].Match(member)) { throw new InvalidOperationException(Res.GetString("XmlInvalidXmlOverride", new object[] { parent, member.Name, base.TypeName, this.members[i].Name })); } declaringMapping = this; return this.members[i]; } } } return null; }
internal Member(XmlSerializationReaderILGen outerClass, string source, string arraySource, string arrayName, int i, MemberMapping mapping, bool multiRef, string choiceSource) { this.source = source; this.arrayName = arrayName + "_" + i.ToString(CultureInfo.InvariantCulture); this.choiceArrayName = "choice_" + this.arrayName; this.choiceSource = choiceSource; if (mapping.TypeDesc.IsArrayLike) { if (arraySource != null) this.arraySource = arraySource; else this.arraySource = outerClass.GetArraySource(mapping.TypeDesc, this.arrayName, multiRef); isArray = mapping.TypeDesc.IsArray; isList = !isArray; if (mapping.ChoiceIdentifier != null) { this.choiceArraySource = outerClass.GetArraySource(mapping.TypeDesc, this.choiceArrayName, multiRef); string a = choiceArrayName; string c = "c" + a; string choiceTypeFullName = mapping.ChoiceIdentifier.Mapping.TypeDesc.CSharpName; string castString = "(" + choiceTypeFullName + "[])"; string init = a + " = " + castString + "EnsureArrayIndex(" + a + ", " + c + ", " + outerClass.RaCodeGen.GetStringForTypeof(choiceTypeFullName) + ");"; this.choiceArraySource = init + outerClass.RaCodeGen.GetStringForArrayMember(a, c + "++", mapping.ChoiceIdentifier.Mapping.TypeDesc); } else { this.choiceArraySource = this.choiceSource; } } else { this.arraySource = arraySource == null ? source : arraySource; this.choiceArraySource = this.choiceSource; } this.mapping = mapping; }
private int FindXmlnsIndex(MemberMapping[] members) { for (int i = 0; i < members.Length; i++) { if (members[i].Xmlns != null) { return i; } } return -1; }
private void AddMemberMetadata(CodeAttributeDeclarationCollection metadata, MemberMapping member, bool forceUseMemberName) { if (member.Elements.Length != 0) { ElementAccessor accessor = member.Elements[0]; TypeMapping mapping = accessor.Mapping; string str = Accessor.UnescapeName(accessor.Name); bool flag = (str == member.Name) && !forceUseMemberName; if ((!flag || mapping.TypeDesc.IsAmbiguousDataType) || accessor.IsNullable) { this.AddElementMetadata(metadata, flag ? null : str, mapping.TypeDesc.IsAmbiguousDataType ? mapping.TypeDesc : null, accessor.IsNullable); } } }
private static void AddUniqueAccessor(MemberMapping member, INameScope elements, INameScope attributes, bool isSequence) { if (member.Attribute != null) { AddUniqueAccessor(attributes, member.Attribute); } else if ((!isSequence && (member.Elements != null)) && (member.Elements.Length > 0)) { for (int i = 0; i < member.Elements.Length; i++) { AddUniqueAccessor(elements, member.Elements[i]); } } }
private bool WriteMemberText(out object o, Member anyText) { MemberMapping anyTextMapping = anyText.Mapping; if ((Reader.NodeType == XmlNodeType.Text || Reader.NodeType == XmlNodeType.CDATA || Reader.NodeType == XmlNodeType.Whitespace || Reader.NodeType == XmlNodeType.SignificantWhitespace)) { TextAccessor text = anyTextMapping.Text; if (text.Mapping is SpecialMapping) { // #10592: To Support text.Mapping being SpecialMapping throw new NotImplementedException("text.Mapping is SpecialMapping"); } else { if (anyTextMapping.TypeDesc.IsArrayLike) { if (text.Mapping.TypeDesc.CollapseWhitespace) { o = CollapseWhitespace(ReadString()); } else { o = ReadString(); } } else { if (text.Mapping.TypeDesc == StringTypeDesc || text.Mapping.TypeDesc.FormatterName == "String") { o = ReadString(null, text.Mapping.TypeDesc.CollapseWhitespace); } else { o = WritePrimitive(text.Mapping, () => ReadString()); } } } return(true); } o = null; return(false); }
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); }
void AddMemberMetadata(CodeAttributeDeclarationCollection metadata, MemberMapping member, bool forceUseMemberName) { if (member.Elements.Length == 0) { return; } ElementAccessor element = member.Elements[0]; TypeMapping mapping = (TypeMapping)element.Mapping; string elemName = Accessor.UnescapeName(element.Name); bool sameName = ((elemName == member.Name) && !forceUseMemberName); if (!sameName || mapping.TypeDesc.IsAmbiguousDataType || element.IsNullable) { AddElementMetadata(metadata, sameName ? null : elemName, mapping.TypeDesc.IsAmbiguousDataType ? mapping.TypeDesc : null, element.IsNullable); } }
private MemberMapping ImportFieldMapping(FieldModel model, SoapAttributes a, string ns, RecursionLimiter limiter) { if (a.SoapIgnore) { return(null); } MemberMapping accessor = new MemberMapping { IsSoap = true, Name = model.Name, CheckShouldPersist = model.CheckShouldPersist, CheckSpecified = model.CheckSpecified, ReadOnly = model.ReadOnly }; this.ImportAccessorMapping(accessor, model, a, ns, XmlSchemaForm.Unqualified, limiter); return(accessor); }
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); }
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); }
/// <include file='doc\SoapSchemaImporter.uex' path='docs/doc[@for="SoapSchemaImporter.ImportMembersMapping3"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public XmlMembersMapping ImportMembersMapping(string name, string ns, SoapSchemaMember[] members, bool hasWrapperElement, Type baseType, bool baseTypeCanBeIndirect) { XmlSchemaComplexType type = new XmlSchemaComplexType(); XmlSchemaSequence seq = new XmlSchemaSequence(); type.Particle = seq; foreach (SoapSchemaMember member in members) { XmlSchemaElement element = new XmlSchemaElement(); element.Name = member.MemberName; element.SchemaTypeName = member.MemberType; seq.Items.Add(element); } CodeIdentifiers identifiers = new CodeIdentifiers(); identifiers.UseCamelCasing = true; MembersMapping mapping = new MembersMapping(); mapping.TypeDesc = Scope.GetTypeDesc(typeof(object[])); mapping.Members = ImportTypeMembers(type, ns, identifiers); mapping.HasWrapperElement = hasWrapperElement; if (baseType != null) { for (int i = 0; i < mapping.Members.Length; i++) { MemberMapping member = mapping.Members[i]; if (member.Accessor.Mapping is StructMapping) { MakeDerived((StructMapping)member.Accessor.Mapping, baseType, baseTypeCanBeIndirect); } } } ElementAccessor accessor = new ElementAccessor(); accessor.IsSoap = true; accessor.Name = name; accessor.Namespace = ns; accessor.Mapping = mapping; accessor.IsNullable = false; accessor.Form = XmlSchemaForm.Qualified; return(new XmlMembersMapping(Scope, accessor, XmlMappingAccess.Read | XmlMappingAccess.Write)); }
public int Compare(object o1, object o2) { MemberMapping m1 = (MemberMapping)o1; MemberMapping m2 = (MemberMapping)o2; bool m1Text = m1.IsText; if (m1Text) { if (m2.IsText) { return(0); } return(1); } else if (m2.IsText) { return(-1); } if (m1.SequenceId < 0 && m2.SequenceId < 0) { return(0); } if (m1.SequenceId < 0) { return(1); } if (m2.SequenceId < 0) { return(-1); } if (m1.SequenceId < m2.SequenceId) { return(-1); } if (m1.SequenceId > m2.SequenceId) { return(1); } return(0); }
void ExportMember(CodeTypeDeclaration codeClass, MemberMapping member) { string fieldType = member.GetTypeName(CodeProvider); CodeMemberField field = new CodeMemberField(fieldType, member.Name); field.Attributes = (field.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public; field.Comments.Add(new CodeCommentStatement(Res.GetString(Res.XmlRemarks), true)); codeClass.Members.Add(field); AddMemberMetadata(field.CustomAttributes, member, false); if (member.CheckSpecified != SpecifiedAccessor.None) { field = new CodeMemberField(typeof(bool).FullName, member.Name + "Specified"); field.Attributes = (field.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public; field.Comments.Add(new CodeCommentStatement(Res.GetString(Res.XmlRemarks), true)); CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(typeof(SoapIgnoreAttribute).FullName); field.CustomAttributes.Add(attribute); codeClass.Members.Add(field); } }
void ExportMember(CodeTypeDeclaration codeClass, MemberMapping member, string ns) { CodeAttributeDeclarationCollection metadata = new CodeAttributeDeclarationCollection(); CodeMemberField field = new CodeMemberField(member.TypeDesc.FullName, member.Name); field.Attributes = (field.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public; field.Comments.Add(new CodeCommentStatement("<remarks/>", true)); codeClass.Members.Add(field); AddMemberMetadata(field, metadata, member, ns, false); field.CustomAttributes = metadata; if (member.CheckSpecified) { field = new CodeMemberField(typeof(bool).FullName, member.Name + "Specified"); field.Attributes = (field.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public; field.CustomAttributes = new CodeAttributeDeclarationCollection(); field.Comments.Add(new CodeCommentStatement("<remarks/>", true)); CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(typeof(XmlIgnoreAttribute).FullName); field.CustomAttributes.Add(attribute); codeClass.Members.Add(field); } }
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 XmlMembersMapping ImportMembersMapping(string name, string ns, SoapSchemaMember[] members, bool hasWrapperElement, Type baseType, bool baseTypeCanBeIndirect) { XmlSchemaComplexType type = new XmlSchemaComplexType(); XmlSchemaSequence sequence = new XmlSchemaSequence(); type.Particle = sequence; foreach (SoapSchemaMember member in members) { XmlSchemaElement item = new XmlSchemaElement { Name = member.MemberName, SchemaTypeName = member.MemberType }; sequence.Items.Add(item); } CodeIdentifiers identifiers = new CodeIdentifiers { UseCamelCasing = true }; MembersMapping mapping = new MembersMapping { TypeDesc = base.Scope.GetTypeDesc(typeof(object[])), Members = this.ImportTypeMembers(type, ns, identifiers), HasWrapperElement = hasWrapperElement }; if (baseType != null) { for (int i = 0; i < mapping.Members.Length; i++) { MemberMapping mapping2 = mapping.Members[i]; if (mapping2.Accessor.Mapping is StructMapping) { base.MakeDerived((StructMapping)mapping2.Accessor.Mapping, baseType, baseTypeCanBeIndirect); } } } return(new XmlMembersMapping(base.Scope, new ElementAccessor { IsSoap = true, Name = name, Namespace = ns, Mapping = mapping, IsNullable = false, Form = XmlSchemaForm.Qualified }, XmlMappingAccess.Write | XmlMappingAccess.Read)); }
private object GenerateTypeElement(XmlTypeMapping xmlTypeMapping) { ElementAccessor element = xmlTypeMapping.Accessor; TypeMapping mapping = element.Mapping; Reader.MoveToContent(); MemberMapping member = new MemberMapping(); member.TypeDesc = mapping.TypeDesc; member.Elements = new ElementAccessor[] { element }; UnknownNodeAction elementElseAction = UnknownNodeAction.CreateUnknownNodeException; UnknownNodeAction elseAction = UnknownNodeAction.ReadUnknownNode; object o = null; Member tempMember = null; var currentMember = new Member(member); WriteMemberElements(ref o, null /*collectionMember*/, out tempMember, new Member[] { currentMember }, elementElseAction, elseAction, element.Any ? currentMember : null, null, xmlTypeMapping); return(o); }
private void ExportMember(CodeTypeDeclaration codeClass, MemberMapping member, string ns, CodeConstructor ctor) { CodeMemberField field; field = new CodeMemberField(member.GetTypeName(base.CodeProvider), member.Name) { Attributes = (field.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public }; field.Comments.Add(new CodeCommentStatement(Res.GetString("XmlRemarks"), true)); codeClass.Members.Add(field); this.AddMemberMetadata(field, field.CustomAttributes, member, ns, false, field.Comments, ctor); if (member.CheckSpecified != SpecifiedAccessor.None) { field = new CodeMemberField(typeof(bool).FullName, member.Name + "Specified") { Attributes = (field.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public }; field.Comments.Add(new CodeCommentStatement(Res.GetString("XmlRemarks"), true)); CodeAttributeDeclaration declaration = new CodeAttributeDeclaration(typeof(XmlIgnoreAttribute).FullName); field.CustomAttributes.Add(declaration); codeClass.Members.Add(field); } }
public int Compare(object o1, object o2) { MemberMapping mapping = (MemberMapping) o1; MemberMapping mapping2 = (MemberMapping) o2; if (mapping.IsText) { if (mapping2.IsText) { return 0; } return 1; } if (mapping2.IsText) { return -1; } if ((mapping.SequenceId >= 0) || (mapping2.SequenceId >= 0)) { if (mapping.SequenceId < 0) { return 1; } if (mapping2.SequenceId < 0) { return -1; } if (mapping.SequenceId < mapping2.SequenceId) { return -1; } if (mapping.SequenceId > mapping2.SequenceId) { return 1; } } return 0; }
private void WriteMappingInfo(TypeMapping mapping, string typeVariable, Type type) { string cSharpName = mapping.TypeDesc.CSharpName; if (mapping is StructMapping) { StructMapping mapping2 = mapping as StructMapping; for (int i = 0; i < mapping2.Members.Length; i++) { MemberMapping mapping3 = mapping2.Members[i]; this.WriteMemberInfo(type, cSharpName, typeVariable, mapping3.Name); if (mapping3.CheckShouldPersist) { string memberName = "ShouldSerialize" + mapping3.Name; this.WriteMethodInfo(cSharpName, typeVariable, memberName, false, new string[0]); } if (mapping3.CheckSpecified != SpecifiedAccessor.None) { string str3 = mapping3.Name + "Specified"; this.WriteMemberInfo(type, cSharpName, typeVariable, str3); } if (mapping3.ChoiceIdentifier != null) { string str4 = mapping3.ChoiceIdentifier.MemberName; this.WriteMemberInfo(type, cSharpName, typeVariable, str4); } } } else if (mapping is EnumMapping) { FieldInfo[] fields = type.GetFields(); for (int j = 0; j < fields.Length; j++) { this.WriteMemberInfo(type, cSharpName, typeVariable, fields[j].Name); } } }
private void ExportTypeMembers(XmlSchemaComplexType type, MemberMapping[] members, string ns) { XmlSchemaGroupBase group = new XmlSchemaSequence(); for (int i = 0; i < members.Length; i++) { MemberMapping mapping = members[i]; if (mapping.Elements.Length > 0) { bool valueTypeOptional = ((mapping.CheckSpecified != SpecifiedAccessor.None) || mapping.CheckShouldPersist) || !mapping.TypeDesc.IsValueType; this.ExportElementAccessors(group, mapping.Elements, false, valueTypeOptional, ns); } } if (group.Items.Count > 0) { if (type.ContentModel != null) { if (type.ContentModel.Content is XmlSchemaComplexContentExtension) { ((XmlSchemaComplexContentExtension)type.ContentModel.Content).Particle = group; } else { if (!(type.ContentModel.Content is XmlSchemaComplexContentRestriction)) { throw new InvalidOperationException(Res.GetString("XmlInvalidContent", new object[] { type.ContentModel.Content.GetType().Name })); } ((XmlSchemaComplexContentRestriction)type.ContentModel.Content).Particle = group; } } else { type.Particle = group; } } }
void ExportTypeMembers(XmlSchemaComplexType type, MemberMapping[] members, string ns) { XmlSchemaGroupBase seq = new XmlSchemaSequence(); for (int i = 0; i < members.Length; i++) { MemberMapping member = members[i]; if (member.Elements.Length > 0) { bool valueTypeOptional = member.CheckSpecified || member.CheckShouldPersist || !member.TypeDesc.IsValueType; ExportElementAccessors(seq, member.Elements, false, valueTypeOptional, ns); } } if (seq.Items.Count > 0) { if (type.ContentModel != null) { if (type.ContentModel.Content is XmlSchemaComplexContentExtension) { ((XmlSchemaComplexContentExtension)type.ContentModel.Content).Particle = seq; } else if (type.ContentModel.Content is XmlSchemaComplexContentRestriction) { ((XmlSchemaComplexContentRestriction)type.ContentModel.Content).Particle = seq; } else { throw new InvalidOperationException(Res.GetString(Res.XmlInvalidContent, type.ContentModel.Content.GetType().Name)); } } else { type.Particle = seq; } } }
void WriteArray(string source, string arrayName, ArrayMapping arrayMapping, bool readOnly, bool isNullable, int fixupIndex) { if (arrayMapping.IsSoap) { Writer.Write("object rre = "); Writer.Write(fixupIndex >= 0 ? "ReadReferencingElement" : "ReadReferencedElement"); Writer.Write("("); WriteID(arrayMapping.TypeName); Writer.Write(", "); WriteID(arrayMapping.Namespace); if (fixupIndex >= 0) { Writer.Write(", "); Writer.Write("out fixup.Ids["); Writer.Write((fixupIndex).ToString(CultureInfo.InvariantCulture)); Writer.Write("]"); } Writer.WriteLine(");"); TypeDesc td = arrayMapping.TypeDesc; if (td.IsEnumerable || td.IsCollection) { Writer.WriteLine("if (rre != null) {"); Writer.Indent++; WriteAddCollectionFixup(td, readOnly, source, "rre"); Writer.Indent--; Writer.WriteLine("}"); } else { Writer.WriteLine("try {"); Writer.Indent++; WriteSourceBeginTyped(source, arrayMapping.TypeDesc); Writer.Write("rre"); WriteSourceEnd(source); Writer.WriteLine(";"); WriteCatchCastException(arrayMapping.TypeDesc, "rre", null); } } else { Writer.WriteLine("if (!ReadNull()) {"); Writer.Indent++; MemberMapping memberMapping = new MemberMapping(); memberMapping.Elements = arrayMapping.Elements; memberMapping.TypeDesc = arrayMapping.TypeDesc; memberMapping.ReadOnly = readOnly; Member member = new Member(this, source, arrayName, 0, memberMapping, false); member.IsNullable = false;//Note, [....]: IsNullable is set to false since null condition (xsi:nil) is already handled by 'ReadNull()' Member[] members = new Member[] { member }; WriteMemberBegin(members); if (readOnly) { Writer.Write("if (((object)("); Writer.Write(member.ArrayName); Writer.Write(") == null) || "); } else { Writer.Write("if ("); } Writer.WriteLine("(Reader.IsEmptyElement)) {"); Writer.Indent++; Writer.WriteLine("Reader.Skip();"); Writer.Indent--; Writer.WriteLine("}"); Writer.WriteLine("else {"); Writer.Indent++; Writer.WriteLine("Reader.ReadStartElement();"); int loopIndex = WriteWhileNotLoopStart(); Writer.Indent++; string unknownNode = "UnknownNode(null, " + ExpectedElements(members) + ");"; WriteMemberElements(members, unknownNode, unknownNode, null, null, null); Writer.WriteLine("Reader.MoveToContent();"); WriteWhileLoopEnd(loopIndex); Writer.Indent--; Writer.WriteLine("ReadEndElement();"); Writer.WriteLine("}"); WriteMemberEnd(members, false); Writer.Indent--; Writer.WriteLine("}"); if (isNullable) { Writer.WriteLine("else {"); Writer.Indent++; member.IsNullable = true; WriteMemberBegin(members); WriteMemberEnd(members); Writer.Indent--; Writer.WriteLine("}"); } } }
string GenerateTypeElement(XmlTypeMapping xmlTypeMapping) { ElementAccessor element = xmlTypeMapping.Accessor; TypeMapping mapping = element.Mapping; string methodName = NextMethodName(element.Name); Writer.WriteLine(); Writer.Write("public object "); Writer.Write(methodName); Writer.WriteLine("() {"); Writer.Indent++; Writer.WriteLine("object o = null;"); MemberMapping member = new MemberMapping(); member.TypeDesc = mapping.TypeDesc; //member.ReadOnly = !mapping.TypeDesc.HasDefaultConstructor; member.Elements = new ElementAccessor[] { element }; Member[] members = new Member[] { new Member(this,"o", "o", "a", 0, member) }; Writer.WriteLine("Reader.MoveToContent();"); string unknownNode = "UnknownNode(null, " + ExpectedElements(members) + ");"; WriteMemberElements(members, "throw CreateUnknownNodeException();", unknownNode, element.Any ? members[0] : null, null, null); if (element.IsSoap) { Writer.WriteLine("Referenced(o);"); Writer.WriteLine("ReadReferencedElements();"); } Writer.WriteLine("return (object)o;"); Writer.Indent--; Writer.WriteLine("}"); return methodName; }
string GetChoiceIdentifierSource(MemberMapping mapping, string parent, TypeDesc parentTypeDesc) { if (mapping.ChoiceIdentifier == null) return ""; CodeIdentifier.CheckValidIdentifier(mapping.ChoiceIdentifier.MemberName); return RaCodeGen.GetStringForMember(parent, mapping.ChoiceIdentifier.MemberName, parentTypeDesc); }
internal Member(XmlSerializationReaderCodeGen outerClass, string source, string arrayName, int i, MemberMapping mapping, bool multiRef) : this(outerClass, source, null, arrayName, i, mapping, multiRef, null) { }
internal XmlMemberMapping(MemberMapping mapping) { this.mapping = mapping; }
void AddMemberMetadata(CodeAttributeDeclarationCollection metadata, MemberMapping member, bool forceUseMemberName) { if (member.Elements.Length == 0) return; ElementAccessor element = member.Elements[0]; TypeMapping mapping = (TypeMapping)element.Mapping; string elemName = Accessor.UnescapeName(element.Name); bool sameName = ((elemName == member.Name) && !forceUseMemberName); if (!sameName || mapping.TypeDesc.IsAmbiguousDataType || element.IsNullable) { AddElementMetadata(metadata, sameName ? null : elemName, mapping.TypeDesc.IsAmbiguousDataType ? mapping.TypeDesc : null, element.IsNullable); } }
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; }
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); }
private MemberMapping(MemberMapping mapping) : base(mapping) { _name = mapping._name; _checkShouldPersist = mapping._checkShouldPersist; _checkSpecified = mapping._checkSpecified; _isReturnValue = mapping._isReturnValue; _readOnly = mapping._readOnly; _sequenceId = mapping._sequenceId; _memberInfo = mapping._memberInfo; _checkSpecifiedMemberInfo = mapping._checkSpecifiedMemberInfo; _checkShouldPersistMethodInfo = mapping._checkShouldPersistMethodInfo; }
internal bool Declares(MemberMapping member, string parent) { StructMapping m; return (FindDeclaringMapping(member, out m, parent) != null); }
internal MemberMapping FindDeclaringMapping(MemberMapping member, out StructMapping declaringMapping, string parent) { declaringMapping = null; if (BaseMapping != null) { MemberMapping baseMember = BaseMapping.FindDeclaringMapping(member, out declaringMapping, parent); if (baseMember != null) return baseMember; } if (_members == null) return null; for (int i = 0; i < _members.Length; i++) { if (_members[i].Name == member.Name) { if (_members[i].TypeDesc != member.TypeDesc) throw new InvalidOperationException(SR.Format(SR.XmlHiddenMember, parent, member.Name, member.TypeDesc.FullName, this.TypeName, _members[i].Name, _members[i].TypeDesc.FullName)); else if (!_members[i].Match(member)) { throw new InvalidOperationException(SR.Format(SR.XmlInvalidXmlOverride, parent, member.Name, this.TypeName, _members[i].Name)); } declaringMapping = this; return _members[i]; } } return null; }
private void WriteStructMethod(StructMapping mapping, string n, string ns, object o, bool isNullable, bool needType, XmlMapping parentMapping = null) { if (mapping.IsSoap && mapping.TypeDesc.IsRoot) { return; } if (mapping.IsSoap) { } else { if (o == null) { if (isNullable) { WriteNullTagLiteral(n, ns); } return; } if (!needType && o.GetType() != mapping.TypeDesc.Type) { if (WriteDerivedTypes(mapping, n, ns, o, isNullable)) { return; } if (mapping.TypeDesc.IsRoot) { if (WriteEnumAndArrayTypes(mapping, o, n, ns, parentMapping)) { return; } WriteTypedPrimitive(n, ns, o, true); return; } throw CreateUnknownTypeException(o); } } if (!mapping.TypeDesc.IsAbstract) { if (mapping.TypeDesc.Type != null && typeof(XmlSchemaObject).IsAssignableFrom(mapping.TypeDesc.Type)) { EscapeName = false; } XmlSerializerNamespaces xmlnsSource = null; MemberMapping[] members = TypeScope.GetAllMembers(mapping); int xmlnsMember = FindXmlnsIndex(members); if (xmlnsMember >= 0) { MemberMapping member = members[xmlnsMember]; xmlnsSource = (XmlSerializerNamespaces)GetMemberValue(o, member.Name); } if (!mapping.IsSoap) { WriteStartElement(n, ns, o, false, xmlnsSource); if (!mapping.TypeDesc.IsRoot) { if (needType) { WriteXsiType(mapping.TypeName, mapping.Namespace); } } } else if (xmlnsSource != null) { WriteNamespaceDeclarations(xmlnsSource); } for (int i = 0; i < members.Length; i++) { MemberMapping m = members[i]; string memberName = m.Name; object memberValue = GetMemberValue(o, memberName); bool isSpecified = true; bool shouldPersist = true; if (m.CheckSpecified != SpecifiedAccessor.None) { string specifiedMemberName = m.Name + "Specified"; isSpecified = (bool)GetMemberValue(o, specifiedMemberName); } if (m.CheckShouldPersist) { string methodInvoke = "ShouldSerialize" + m.Name; MethodInfo method = o.GetType().GetTypeInfo().GetDeclaredMethod(methodInvoke); shouldPersist = (bool)method.Invoke(o, Array.Empty <object>()); } if (m.Attribute != null) { if (isSpecified && shouldPersist) { WriteMember(memberValue, m.Attribute, m.TypeDesc, o); } } } for (int i = 0; i < members.Length; i++) { MemberMapping m = members[i]; string memberName = m.Name; object memberValue = GetMemberValue(o, memberName); bool isSpecified = true; bool shouldPersist = true; if (m.CheckSpecified != SpecifiedAccessor.None) { string specifiedMemberName = m.Name + "Specified"; isSpecified = (bool)GetMemberValue(o, specifiedMemberName); } if (m.CheckShouldPersist) { string methodInvoke = "ShouldSerialize" + m.Name; MethodInfo method = o.GetType().GetTypeInfo().GetDeclaredMethod(methodInvoke); shouldPersist = (bool)method.Invoke(o, Array.Empty <object>()); } if (m.Xmlns != null) { continue; } bool checkShouldPersist = m.CheckShouldPersist && (m.Elements.Length > 0 || m.Text != null); if (!checkShouldPersist) { shouldPersist = true; } object choiceSource = null; if (m.ChoiceIdentifier != null) { choiceSource = GetMemberValue(o, m.ChoiceIdentifier.MemberName); } if (isSpecified && shouldPersist) { WriteMember(memberValue, choiceSource, m.ElementsSortedByDerivation, m.Text, m.ChoiceIdentifier, m.TypeDesc, true, parentMapping); } } if (!mapping.IsSoap) { WriteEndElement(o); } } }
void ExportMember(CodeTypeDeclaration codeClass, MemberMapping member) { CodeAttributeDeclarationCollection metadata = new CodeAttributeDeclarationCollection(); CodeMemberField field = new CodeMemberField(member.TypeDesc.FullName, member.Name); field.Attributes = (field.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public; field.Comments.Add(new CodeCommentStatement("<remarks/>", true)); codeClass.Members.Add(field); AddMemberMetadata(metadata, member, false); field.CustomAttributes = metadata; }
private static void PopulateMemberInfos(StructMapping structMapping, MemberMapping[] memberMappings, System.Collections.Generic.Dictionary<string, MemberInfo> memberInfos) { memberInfos.Clear(); for (int i = 0; i < memberMappings.Length; ++i) { memberInfos[memberMappings[i].Name] = memberMappings[i].MemberInfo; if (memberMappings[i].ChoiceIdentifier != null) memberInfos[memberMappings[i].ChoiceIdentifier.MemberName] = memberMappings[i].ChoiceIdentifier.MemberInfo; if (memberMappings[i].CheckSpecifiedMemberInfo != null) memberInfos[memberMappings[i].Name + "Specified"] = memberMappings[i].CheckSpecifiedMemberInfo; } // The scenario here is that user has one base class A and one derived class B and wants to serialize/deserialize an object of B. // There's one virtual property defined in A and overrided by B. Without the replacing logic below, the code generated will always // try to access the property defined in A, rather than B. // The logic here is to: // 1) Check current members inside memberInfos dictionary and figure out whether there's any override or new properties defined in the derived class. // If so, replace the one on the base class with the one on the derived class. // 2) Do the same thing for the memberMapping array. Note that we need to create a new copy of MemberMapping object since the old one could still be referenced // by the StructMapping of the baseclass, so updating it directly could lead to other issues. Dictionary<string, MemberInfo> replaceList = null; MemberInfo replacedInfo = null; foreach (KeyValuePair<string, MemberInfo> pair in memberInfos) { if (ShouldBeReplaced(pair.Value, structMapping.TypeDesc.Type, out replacedInfo)) { if (replaceList == null) { replaceList = new Dictionary<string, MemberInfo>(); } replaceList.Add(pair.Key, replacedInfo); } } if (replaceList != null) { foreach (KeyValuePair<string, MemberInfo> pair in replaceList) { memberInfos[pair.Key] = pair.Value; } for (int i = 0; i < memberMappings.Length; i++) { MemberInfo mi; if (replaceList.TryGetValue(memberMappings[i].Name, out mi)) { MemberMapping newMapping = memberMappings[i].Clone(); newMapping.MemberInfo = mi; memberMappings[i] = newMapping; } } } }
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 }; } }
private void ExportTypeMembers(XmlSchemaComplexType type, MemberMapping[] members, string name, string ns, bool hasSimpleContent, bool openModel) { XmlSchemaGroupBase group = new XmlSchemaSequence(); TypeMapping mapping = null; for (int i = 0; i < members.Length; i++) { MemberMapping mapping2 = members[i]; if (!mapping2.Ignore) { if (mapping2.Text != null) { if (mapping != null) { throw new InvalidOperationException(Res.GetString("XmlIllegalMultipleText", new object[] { name })); } mapping = mapping2.Text.Mapping; } if (mapping2.Elements.Length > 0) { bool repeats = mapping2.TypeDesc.IsArrayLike && ((mapping2.Elements.Length != 1) || !(mapping2.Elements[0].Mapping is ArrayMapping)); bool valueTypeOptional = (mapping2.CheckSpecified != SpecifiedAccessor.None) || mapping2.CheckShouldPersist; this.ExportElementAccessors(group, mapping2.Elements, repeats, valueTypeOptional, ns); } } } if (group.Items.Count > 0) { if (type.ContentModel != null) { if (type.ContentModel.Content is XmlSchemaComplexContentRestriction) { ((XmlSchemaComplexContentRestriction) type.ContentModel.Content).Particle = group; } else { if (!(type.ContentModel.Content is XmlSchemaComplexContentExtension)) { throw new InvalidOperationException(Res.GetString("XmlInvalidContent", new object[] { type.ContentModel.Content.GetType().Name })); } ((XmlSchemaComplexContentExtension) type.ContentModel.Content).Particle = group; } } else { type.Particle = group; } } if (mapping != null) { if (hasSimpleContent) { if ((mapping is PrimitiveMapping) && (group.Items.Count == 0)) { PrimitiveMapping mapping3 = (PrimitiveMapping) mapping; if (mapping3.IsList) { type.IsMixed = true; } else { if (mapping3.IsAnonymousType) { throw new InvalidOperationException(Res.GetString("XmlAnonymousBaseType", new object[] { mapping.TypeDesc.Name, mapping3.TypeDesc.Name, "AnonymousType", "false" })); } XmlSchemaSimpleContent content = new XmlSchemaSimpleContent(); XmlSchemaSimpleContentExtension extension = new XmlSchemaSimpleContentExtension(); content.Content = extension; type.ContentModel = content; extension.BaseTypeName = this.ExportPrimitiveMapping(mapping3, ns); } } } else { type.IsMixed = true; } } bool flag3 = false; for (int j = 0; j < members.Length; j++) { if (members[j].Attribute != null) { this.ExportAttributeAccessor(type, members[j].Attribute, (members[j].CheckSpecified != SpecifiedAccessor.None) || members[j].CheckShouldPersist, ns); if (members[j].Attribute.Any) { flag3 = true; } } } if (openModel && !flag3) { AttributeAccessor accessor = new AttributeAccessor { Any = true }; this.ExportAttributeAccessor(type, accessor, false, ns); } }
internal bool Declares(MemberMapping member, string parent) { StructMapping m; return(FindDeclaringMapping(member, out m, parent) != null); }
private void WriteAttributes(MemberMapping[] members, MemberMapping anyAttribute, Action <object> elseCall, ref object o) { MemberMapping xmlnsMember = null; var attributes = new List <AttributeAccessor>(); while (Reader.MoveToNextAttribute()) { bool memberFound = false; foreach (var member in members) { if (member.Xmlns != null) { xmlnsMember = member; continue; } if (member.Ignore) { continue; } AttributeAccessor attribute = member.Attribute; if (attribute == null) { continue; } if (attribute.Any) { continue; } attributes.Add(attribute); if (attribute.IsSpecialXmlNamespace) { memberFound = XmlNodeEqual(Reader, attribute.Name, XmlReservedNs.NsXml); } else { memberFound = XmlNodeEqual(Reader, attribute.Name, attribute.Form == XmlSchemaForm.Qualified ? attribute.Namespace : ""); } if (memberFound) { WriteAttribute(o, member); memberFound = true; break; } } if (memberFound) { continue; } bool flag2 = false; if (xmlnsMember != null) { if (IsXmlnsAttribute(Reader.Name)) { if (GetMemberType(xmlnsMember.MemberInfo) == typeof(XmlSerializerNamespaces)) { var xmlnsMemberSource = GetMemberValue(o, xmlnsMember.MemberInfo) as XmlSerializerNamespaces; if (xmlnsMemberSource == null) { xmlnsMemberSource = new XmlSerializerNamespaces(); SetMemberValue(o, xmlnsMemberSource, xmlnsMember.MemberInfo); } xmlnsMemberSource.Add(Reader.Name.Length == 5 ? "" : Reader.LocalName, Reader.Value); } else { throw new InvalidOperationException("xmlnsMemberSource is not of type XmlSerializerNamespaces"); } } else { flag2 = true; } } else if (!IsXmlnsAttribute(Reader.Name)) { flag2 = true; } if (flag2) { if (anyAttribute != null) { var attr = Document.ReadNode(Reader) as XmlAttribute; ParseWsdlArrayType(attr); WriteAttribute(o, anyAttribute, attr); } else { elseCall(o); } } } }
internal Member(XmlSerializationReaderCodeGen outerClass, string source, string arraySource, string arrayName, int i, MemberMapping mapping, string choiceSource) : this (outerClass, source, arraySource, arrayName, i, mapping, false, choiceSource) { }
public Member(MemberMapping mapping) { Mapping = mapping; }
string GetChoiceIdentifierSource(MemberMapping[] mappings, MemberMapping member) { string choiceSource = null; if (member.ChoiceIdentifier != null) { for (int j = 0; j < mappings.Length; j++) { if (mappings[j].Name == member.ChoiceIdentifier.MemberName) { choiceSource = "p[" + j.ToString(CultureInfo.InvariantCulture) + "]"; break; } } #if DEBUG // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe if (choiceSource == null) throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Can not find " + member.ChoiceIdentifier.MemberName + " in the members mapping.")); #endif } return choiceSource; }
public Member(MemberMapping mapping, CollectionMember collectionMember) : this(mapping) { Collection = collectionMember; }
void InitializeValueTypes(string arrayName, MemberMapping[] mappings) { for (int i = 0; i < mappings.Length; i++) { if (!mappings[i].TypeDesc.IsValueType) continue; Writer.Write(arrayName); Writer.Write("["); Writer.Write(i.ToString(CultureInfo.InvariantCulture)); Writer.Write("] = "); if (mappings[i].TypeDesc.IsOptionalValue && mappings[i].TypeDesc.BaseTypeDesc.UseReflection) { Writer.Write("null"); } else { Writer.Write(RaCodeGen.GetStringForCreateInstance(mappings[i].TypeDesc.CSharpName, mappings[i].TypeDesc.UseReflection, false, false)); } Writer.WriteLine(";"); } }
MemberMapping(MemberMapping mapping) : base(mapping) { this.name = mapping.name; this.checkShouldPersist = mapping.checkShouldPersist; this.checkSpecified = mapping.checkSpecified; this.isReturnValue = mapping.isReturnValue; this.readOnly = mapping.readOnly; this.sequenceId = mapping.sequenceId; this.memberInfo = mapping.memberInfo; this.checkSpecifiedMemberInfo = mapping.checkSpecifiedMemberInfo; this.checkShouldPersistMethodInfo = mapping.checkShouldPersistMethodInfo; }
void WriteEnumAndArrayTypes() { foreach (TypeScope scope in Scopes) { foreach (Mapping m in scope.TypeMappings) { if (m.IsSoap) continue; if (m is EnumMapping) { EnumMapping mapping = (EnumMapping)m; Writer.Write("else if ("); WriteQNameEqual("xsiType", mapping.TypeName, mapping.Namespace); Writer.WriteLine(") {"); Writer.Indent++; Writer.WriteLine("Reader.ReadStartElement();"); string methodName = ReferenceMapping(mapping); #if DEBUG // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe if (methodName == null) throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorMethod, mapping.TypeDesc.Name)); #endif Writer.Write("object e = "); Writer.Write(methodName); Writer.WriteLine("(CollapseWhitespace(Reader.ReadString()));"); Writer.WriteLine("ReadEndElement();"); Writer.WriteLine("return e;"); Writer.Indent--; Writer.WriteLine("}"); } else if (m is ArrayMapping) { ArrayMapping mapping = (ArrayMapping) m; if (mapping.TypeDesc.HasDefaultConstructor) { Writer.Write("else if ("); WriteQNameEqual("xsiType", mapping.TypeName, mapping.Namespace); Writer.WriteLine(") {"); Writer.Indent++; MemberMapping memberMapping = new MemberMapping(); memberMapping.TypeDesc = mapping.TypeDesc; memberMapping.Elements = mapping.Elements; Member member = new Member(this,"a", "z", 0, memberMapping); TypeDesc td = mapping.TypeDesc; string fullTypeName = mapping.TypeDesc.CSharpName; if (td.UseReflection){ if (td.IsArray) Writer.Write(typeof(Array).FullName); else Writer.Write("object"); } else Writer.Write(fullTypeName); Writer.Write(" a = "); if (mapping.TypeDesc.IsValueType) { Writer.Write(RaCodeGen.GetStringForCreateInstance(fullTypeName, td.UseReflection, false, false)); Writer.WriteLine(";"); } else Writer.WriteLine("null;"); WriteArray(member.Source, member.ArrayName, mapping, false, false, -1); Writer.WriteLine("return a;"); Writer.Indent--; Writer.WriteLine("}"); } } } } }
private object WriteLiteralStructMethod(StructMapping structMapping, bool isNullable, bool checkType, string defaultNamespace) { XmlQualifiedName xsiType = checkType ? GetXsiType() : null; bool isNull = false; if (isNullable) { isNull = ReadNull(); } if (checkType) { if (structMapping.TypeDesc.IsRoot && isNull) { if (xsiType != null) { return(ReadTypedNull(xsiType)); } else { if (structMapping.TypeDesc.IsValueType) { return(ReflectionCreateObject(structMapping.TypeDesc.Type)); } else { return(null); } } } object o = null; if (xsiType == null || (!structMapping.TypeDesc.IsRoot && QNameEqual(xsiType, structMapping.TypeName, structMapping.Namespace, defaultNamespace))) { if (structMapping.TypeDesc.IsRoot) { return(ReadTypedPrimitive(new XmlQualifiedName(Soap.UrType, XmlSchemaConstants.Namespace))); } } else if (WriteDerivedTypes(out o, structMapping, xsiType, defaultNamespace, checkType, isNullable)) { return(o); } else if (structMapping.TypeDesc.IsRoot && WriteEnumAndArrayTypes(out o, structMapping, xsiType, defaultNamespace)) { return(o); } else { if (structMapping.TypeDesc.IsRoot) { return(ReadTypedPrimitive(xsiType)); } else { throw CreateUnknownTypeException(xsiType); } } } if (structMapping.TypeDesc.IsNullable && isNull) { return(null); } else if (structMapping.TypeDesc.IsAbstract) { throw CreateAbstractTypeException(structMapping.TypeName, structMapping.Namespace); } else { if (structMapping.TypeDesc.Type != null && typeof(XmlSchemaObject).IsAssignableFrom(structMapping.TypeDesc.Type)) { // #10589: To Support Serializing XmlSchemaObject throw new NotImplementedException("typeof(XmlSchemaObject)"); } object o = ReflectionCreateObject(structMapping.TypeDesc.Type); MemberMapping[] mappings = TypeScope.GetSettableMembers(structMapping); MemberMapping anyText = null; MemberMapping anyElement = null; MemberMapping anyAttribute = null; Member anyElementMember = null; Member anyTextMember = null; bool isSequence = structMapping.HasExplicitSequence(); if (isSequence) { // #10586: Currently the reflection based method treat this kind of type as normal types. // But potentially we can do some optimization for types that have ordered properties. } var allMembersList = new List <Member>(mappings.Length); var allMemberMappingList = new List <MemberMapping>(mappings.Length); for (int i = 0; i < mappings.Length; i++) { MemberMapping mapping = mappings[i]; if (mapping.Text != null) { anyText = mapping; } if (mapping.Attribute != null && mapping.Attribute.Any) { anyAttribute = mapping; } if (!isSequence) { // find anyElement if present. for (int j = 0; j < mapping.Elements.Length; j++) { if (mapping.Elements[j].Any && (mapping.Elements[j].Name == null || mapping.Elements[j].Name.Length == 0)) { anyElement = mapping; break; } } } else if (mapping.IsParticle && !mapping.IsSequence) { StructMapping declaringMapping; structMapping.FindDeclaringMapping(mapping, out declaringMapping, structMapping.TypeName); throw new InvalidOperationException(SR.Format(SR.XmlSequenceHierarchy, structMapping.TypeDesc.FullName, mapping.Name, declaringMapping.TypeDesc.FullName, "Order")); } var member = new Member(mapping); if (mapping.TypeDesc.IsArrayLike) { if (mapping.TypeDesc.IsArrayLike && !(mapping.Elements.Length == 1 && mapping.Elements[0].Mapping is ArrayMapping)) { member.Collection = new CollectionMember(); } else if (!mapping.TypeDesc.IsArray) { } } allMemberMappingList.Add(mapping); allMembersList.Add(member); if (mapping == anyElement) { anyElementMember = member; } else if (mapping == anyText) { anyTextMember = member; } } var allMembers = allMembersList.ToArray(); var allMemberMappings = allMemberMappingList.ToArray(); Action <object> unknownNodeAction = (n) => UnknownNode(n); WriteAttributes(allMemberMappings, anyAttribute, unknownNodeAction, ref o); Reader.MoveToElement(); if (Reader.IsEmptyElement) { Reader.Skip(); return(o); } Reader.ReadStartElement(); bool IsSequenceAllMembers = IsSequence(allMembers); if (IsSequenceAllMembers) { // #10586: Currently the reflection based method treat this kind of type as normal types. // But potentially we can do some optimization for types that have ordered properties. } UnknownNodeAction unknownNode = UnknownNodeAction.ReadUnknownNode; WriteMembers(ref o, allMembers, unknownNode, unknownNode, anyElementMember, anyTextMember); ReadEndElement(); return(o); } }
void AddMemberMetadata(CodeMemberField field, CodeAttributeDeclarationCollection metadata, MemberMapping member, string ns, bool forceUseMemberName) { if (member.Xmlns != null) { CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(typeof(XmlNamespaceDeclarationsAttribute).FullName); metadata.Add(attribute); } else if (member.Attribute != null) { AttributeAccessor attribute = member.Attribute; if (attribute.Any) { ExportAnyAttribute(metadata); } else { TypeMapping mapping = (TypeMapping)attribute.Mapping; string attrName = Accessor.UnescapeName(attribute.Name); bool sameType = mapping.TypeDesc == member.TypeDesc || (member.TypeDesc.IsArrayLike && mapping.TypeDesc == member.TypeDesc.ArrayElementTypeDesc); bool sameName = attrName == member.Name && !forceUseMemberName; bool sameNs = attribute.Namespace == ns; bool defaultForm = attribute.Form != XmlSchemaForm.Qualified; ExportAttribute(metadata, sameName ? null : attrName, sameNs ? null : attribute.Namespace, sameType ? null : mapping.TypeDesc, mapping.TypeDesc, defaultForm ? XmlSchemaForm.None : attribute.Form); if (attribute.HasDefault) { AddDefaultValueAttribute(field, metadata, attribute.Default, mapping); } } } else { if (member.Text != null) { TypeMapping mapping = (TypeMapping)member.Text.Mapping; bool sameType = mapping.TypeDesc == member.TypeDesc || (member.TypeDesc.IsArrayLike && mapping.TypeDesc == member.TypeDesc.ArrayElementTypeDesc); ExportText(metadata, sameType ? null : mapping.TypeDesc, mapping.TypeDesc.IsAmbiguousDataType ? mapping.TypeDesc.DataType.Name : null); } if (member.Elements.Length == 1) { ElementAccessor element = member.Elements[0]; TypeMapping mapping = (TypeMapping)element.Mapping; string elemName = Accessor.UnescapeName(element.Name); bool sameName = ((elemName == member.Name) && !forceUseMemberName); bool isArray = mapping is ArrayMapping; bool sameNs = element.Namespace == ns; bool defaultForm = element.Form != XmlSchemaForm.Unqualified; if (element.Any) { ExportAnyElement(metadata, elemName, sameNs ? null : element.Namespace); } else if (isArray) { bool sameType = mapping.TypeDesc == member.TypeDesc; ArrayMapping array = (ArrayMapping)mapping; if (!sameName || !sameNs || element.IsNullable || !defaultForm) { ExportArray(metadata, sameName ? null : elemName, sameNs ? null : element.Namespace, element.IsNullable, defaultForm ? XmlSchemaForm.None : element.Form); } ExportArrayElements(metadata, array, ns, member.TypeDesc.ArrayElementTypeDesc, 0); } else { bool sameType = mapping.TypeDesc == member.TypeDesc || (member.TypeDesc.IsArrayLike && mapping.TypeDesc == member.TypeDesc.ArrayElementTypeDesc); if (member.TypeDesc.IsArrayLike) { sameName = false; } if (member.TypeDesc.IsAmbiguousDataType || member.TypeDesc.IsArrayLike || !sameName || !sameType || !sameNs || element.IsNullable || !defaultForm) { ExportElement(metadata, sameName ? null : elemName, sameNs ? null : element.Namespace, sameType ? null : mapping.TypeDesc, mapping.TypeDesc, element.IsNullable, defaultForm ? XmlSchemaForm.None : element.Form); } } if (element.HasDefault) { AddDefaultValueAttribute(field, metadata, element.Default, mapping); } } else { for (int i = 0; i < member.Elements.Length; i++) { ElementAccessor element = member.Elements[i]; string elemName = Accessor.UnescapeName(element.Name); bool sameNs = element.Namespace == ns; if (element.Any) { ExportAnyElement(metadata, elemName, sameNs ? null : element.Namespace); } else { bool defaultForm = element.Form != XmlSchemaForm.Unqualified; ExportElement(metadata, elemName, sameNs ? null : element.Namespace, ((TypeMapping)element.Mapping).TypeDesc, ((TypeMapping)element.Mapping).TypeDesc, element.IsNullable, defaultForm ? XmlSchemaForm.None : element.Form); } } } if (member.ChoiceIdentifier != null) { CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(typeof(XmlChoiceIdentifierAttribute).FullName); attribute.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(member.ChoiceIdentifier.MemberName))); metadata.Add(attribute); } if (member.Ignore) { CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(typeof(XmlIgnoreAttribute).FullName); metadata.Add(attribute); } } }
void ExportTypeMembers(XmlSchemaComplexType type, MemberMapping[] members, string name, string ns, bool hasSimpleContent) { XmlSchemaGroupBase seq = new XmlSchemaSequence(); TypeMapping textMapping = null; for (int i = 0; i < members.Length; i++) { MemberMapping member = members[i]; if (member.Ignore) { continue; } if (member.Text != null) { if (textMapping != null) { throw new InvalidOperationException(Res.GetString(Res.XmlIllegalMultipleText, name)); } textMapping = member.Text.Mapping; } if (member.Elements.Length > 0) { bool repeats = member.TypeDesc.IsArrayLike && !(member.Elements.Length == 1 && member.Elements[0].Mapping is ArrayMapping); bool valueTypeOptional = member.CheckSpecified || member.CheckShouldPersist; ExportElementAccessors(seq, member.Elements, repeats, valueTypeOptional, ns); } } if (seq.Items.Count > 0) { if (type.ContentModel != null) { if (type.ContentModel.Content is XmlSchemaComplexContentRestriction) { ((XmlSchemaComplexContentRestriction)type.ContentModel.Content).Particle = seq; } else if (type.ContentModel.Content is XmlSchemaComplexContentExtension) { ((XmlSchemaComplexContentExtension)type.ContentModel.Content).Particle = seq; } else { throw new InvalidOperationException(Res.GetString(Res.XmlInvalidContent, type.ContentModel.Content.GetType().Name)); } } else { type.Particle = seq; } } if (textMapping != null) { if (hasSimpleContent) { if (textMapping is PrimitiveMapping && seq.Items.Count == 0) { PrimitiveMapping pm = (PrimitiveMapping)textMapping; if (pm.IsList) { type.IsMixed = true; } else { // Create simpleContent XmlSchemaSimpleContent model = new XmlSchemaSimpleContent(); XmlSchemaSimpleContentExtension ex = new XmlSchemaSimpleContentExtension(); model.Content = ex; type.ContentModel = model; ex.BaseTypeName = ExportPrimitiveMapping(pm, ns); } } } else { type.IsMixed = true; } } for (int i = 0; i < members.Length; i++) { ExportAttributeAccessor(type, members[i].Attribute, members[i].CheckSpecified || members[i].CheckShouldPersist, ns); } }
internal XmlMemberMapping(MemberMapping mapping) { _mapping = mapping; }
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; }
void ImportElementMember(XmlSchemaElement element, CodeIdentifiers members, string ns) { ElementAccessor accessor; if ((accessor = ImportArray(element, ns)) == null) { accessor = ImportElement(element, ns); } MemberMapping member = new MemberMapping(); member.Name = CodeIdentifier.MakeValid(Accessor.UnescapeName(accessor.Name)); member.Name = members.AddUnique(member.Name, member); if (member.Name.EndsWith("Specified", StringComparison.Ordinal)) { string name = member.Name; member.Name = members.AddUnique(member.Name, member); members.Remove(name); } member.TypeDesc = ((TypeMapping)accessor.Mapping).TypeDesc; member.Elements = new ElementAccessor[] { accessor }; if (element.IsMultipleOccurrence) member.TypeDesc = member.TypeDesc.CreateArrayTypeDesc(); if (element.MinOccurs == 0 && member.TypeDesc.IsValueType && !member.TypeDesc.HasIsEmpty) { member.CheckSpecified = true; } }
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; }
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); }
int FindXmlnsIndex(MemberMapping[] members) { for (int i = 0; i < members.Length; i++) { if (members[i].Xmlns == null) continue; return i; } return -1; }
void ExportProperty(CodeTypeDeclaration codeClass, MemberMapping member, CodeIdentifiers memberScope) { string fieldName = memberScope.AddUnique(CodeExporter.MakeFieldName(member.Name), member); string fieldType = member.GetTypeName(CodeProvider); // need to create a private field CodeMemberField field = new CodeMemberField(fieldType, fieldName); field.Attributes = MemberAttributes.Private; codeClass.Members.Add(field); CodeMemberProperty prop = CreatePropertyDeclaration(field, member.Name, fieldType); prop.Comments.Add(new CodeCommentStatement(Res.GetString(Res.XmlRemarks), true)); AddMemberMetadata(prop.CustomAttributes, member, false); codeClass.Members.Add(prop); if (member.CheckSpecified != SpecifiedAccessor.None) { field = new CodeMemberField(typeof(bool).FullName, fieldName + "Specified"); field.Attributes = MemberAttributes.Private; codeClass.Members.Add(field); prop = CreatePropertyDeclaration(field, member.Name + "Specified", typeof(bool).FullName); prop.Comments.Add(new CodeCommentStatement(Res.GetString(Res.XmlRemarks), true)); CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(typeof(SoapIgnoreAttribute).FullName); prop.CustomAttributes.Add(attribute); codeClass.Members.Add(prop); } }