static private UnescapeName ( string name ) : string | ||
name | string | |
return | string |
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")) { 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(); } }
private void ImportElementMember(XmlSchemaElement element, CodeIdentifiers members, string ns) { MemberMapping mapping; ElementAccessor accessor = this.ImportArray(element, ns); if (accessor == null) { accessor = this.ImportElement(element, ns); } mapping = new MemberMapping { Name = CodeIdentifier.MakeValid(Accessor.UnescapeName(accessor.Name)), Name = members.AddUnique(mapping.Name, mapping) }; if (mapping.Name.EndsWith("Specified", StringComparison.Ordinal)) { string name = mapping.Name; mapping.Name = members.AddUnique(mapping.Name, mapping); members.Remove(name); } mapping.TypeDesc = accessor.Mapping.TypeDesc; mapping.Elements = new ElementAccessor[] { accessor }; if (element.IsMultipleOccurrence) { mapping.TypeDesc = mapping.TypeDesc.CreateArrayTypeDesc(); } if (((element.MinOccurs == 0M) && mapping.TypeDesc.IsValueType) && !mapping.TypeDesc.HasIsEmpty) { mapping.CheckSpecified = SpecifiedAccessor.ReadWrite; } }
private 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 = SpecifiedAccessor.ReadWrite; } }
public void ExportMembersMapping(XmlMembersMapping xmlMembersMapping) { xmlMembersMapping.CheckShallow(); base.CheckScope(xmlMembersMapping.Scope); for (int i = 0; i < xmlMembersMapping.Count; i++) { AccessorMapping mapping = xmlMembersMapping[i].Mapping; if (mapping.Xmlns == null) { if (mapping.Attribute != null) { this.ExportType(mapping.Attribute.Mapping, Accessor.UnescapeName(mapping.Attribute.Name), mapping.Attribute.Namespace, null, false); } if (mapping.Elements != null) { for (int j = 0; j < mapping.Elements.Length; j++) { ElementAccessor accessor = mapping.Elements[j]; this.ExportType(accessor.Mapping, Accessor.UnescapeName(accessor.Name), accessor.Namespace, null, false); } } if (mapping.Text != null) { this.ExportType(mapping.Text.Mapping, Accessor.UnescapeName(mapping.Text.Name), mapping.Text.Namespace, null, false); } } } }
private TypeMapping ImportEnumeratedDataType(XmlSchemaSimpleType dataType, string typeNs, string identifier, bool isList) { TypeMapping mapping = (TypeMapping)base.ImportedMappings[dataType]; if (mapping != null) { return(mapping); } XmlSchemaSimpleType type = this.FindDataType(dataType.DerivedFrom); TypeDesc typeDesc = base.Scope.GetTypeDesc(type); if ((typeDesc != null) && (typeDesc != base.Scope.GetTypeDesc(typeof(string)))) { return(this.ImportPrimitiveDataType(dataType)); } identifier = Accessor.UnescapeName(identifier); string name = base.GenerateUniqueTypeName(identifier); EnumMapping mapping2 = new EnumMapping { IsReference = base.Schemas.IsReference(dataType), TypeDesc = new TypeDesc(name, name, TypeKind.Enum, null, TypeFlags.None), TypeName = identifier, Namespace = typeNs, IsFlags = isList }; CodeIdentifiers identifiers = new CodeIdentifiers(); if (!(dataType.Content is XmlSchemaSimpleTypeRestriction)) { throw new InvalidOperationException(Res.GetString("XmlInvalidEnumContent", new object[] { dataType.Content.GetType().Name, identifier })); } XmlSchemaSimpleTypeRestriction content = (XmlSchemaSimpleTypeRestriction)dataType.Content; for (int i = 0; i < content.Facets.Count; i++) { object obj2 = content.Facets[i]; if (obj2 is XmlSchemaEnumerationFacet) { XmlSchemaEnumerationFacet facet = (XmlSchemaEnumerationFacet)obj2; ConstantMapping mapping3 = new ConstantMapping(); string str2 = CodeIdentifier.MakeValid(facet.Value); mapping3.Name = identifiers.AddUnique(str2, mapping3); mapping3.XmlName = facet.Value; mapping3.Value = i; } } mapping2.Constants = (ConstantMapping[])identifiers.ToArray(typeof(ConstantMapping)); if (isList && (mapping2.Constants.Length > 0x3f)) { mapping = new PrimitiveMapping { TypeDesc = base.Scope.GetTypeDesc(typeof(string)), TypeName = mapping.TypeDesc.DataType.Name }; base.ImportedMappings.Add(dataType, mapping); return(mapping); } base.ImportedMappings.Add(dataType, mapping2); base.Scope.AddTypeMapping(mapping2); return(mapping2); }
public void AddMappingMetadata(CodeAttributeDeclarationCollection metadata, XmlTypeMapping mapping, string ns) { mapping.CheckShallow(); base.CheckScope(mapping.Scope); if (!(mapping.Mapping is StructMapping) && !(mapping.Mapping is EnumMapping)) { this.AddRootMetadata(metadata, mapping.Mapping, Accessor.UnescapeName(mapping.Accessor.Name), mapping.Accessor.Namespace, mapping.Accessor); } }
/// <include file='doc\XmlCodeExporter.uex' path='docs/doc[@for="XmlCodeExporter.AddMappingMetadata"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public void AddMappingMetadata(CodeAttributeDeclarationCollection metadata, XmlTypeMapping mapping, string ns) { // For struct or enum mappings, we generate the XmlRoot on the struct/class/enum. For primitives // or arrays, there is nowhere to generate the XmlRoot, so we generate it elsewhere (on the // method for web services get/post). if (mapping.Mapping is StructMapping || mapping.Mapping is EnumMapping) { return; } AddRootMetadata(metadata, mapping.Mapping, Accessor.UnescapeName(mapping.Accessor.Name), mapping.Accessor.Namespace, mapping.Accessor); }
/// <include file='doc\XmlCodeExporter.uex' path='docs/doc[@for="XmlCodeExporter.ExportMembersMapping"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public void ExportMembersMapping(XmlMembersMapping xmlMembersMapping) { CheckScope(xmlMembersMapping.Scope); CheckNamespace(); for (int i = 0; i < xmlMembersMapping.Count; i++) { Accessor accessor = xmlMembersMapping[i].Accessor; if (!(accessor is XmlnsAccessor)) { ExportType(accessor.Mapping, Accessor.UnescapeName(accessor.Name), accessor.Namespace, null); } } }
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); } } }
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); } }
void ExportArrayElements(CodeAttributeDeclarationCollection metadata, ArrayMapping array, string ns, TypeDesc elementTypeDesc, int nestingLevel) { for (int i = 0; i < array.Elements.Length; i++) { ElementAccessor arrayElement = array.Elements[i]; TypeMapping elementMapping = arrayElement.Mapping; string elementName = Accessor.UnescapeName(arrayElement.Name); bool sameName = elementName == arrayElement.Mapping.TypeName; bool sameElementType = elementMapping.TypeDesc == elementTypeDesc; bool sameElementNs = arrayElement.Namespace == ns; bool sameNullable = arrayElement.IsNullable == elementMapping.TypeDesc.IsNullable; if (!sameName || !sameElementType || !sameElementNs || !sameNullable || arrayElement.Form != XmlSchemaForm.Qualified || nestingLevel > 0) { ExportArrayItem(metadata, sameName ? null : elementName, sameElementNs ? null : arrayElement.Namespace, sameElementType ? null : elementMapping.TypeDesc, elementMapping.TypeDesc, arrayElement.IsNullable, arrayElement.Form, nestingLevel); } if (elementMapping is ArrayMapping) { ExportArrayElements(metadata, (ArrayMapping)elementMapping, ns, elementTypeDesc.ArrayElementTypeDesc, nestingLevel + 1); } } }
private void ExportArrayElements(CodeAttributeDeclarationCollection metadata, ArrayMapping array, string ns, TypeDesc elementTypeDesc, int nestingLevel) { for (int i = 0; i < array.Elements.Length; i++) { ElementAccessor accessor = array.Elements[i]; TypeMapping mapping = accessor.Mapping; string str = Accessor.UnescapeName(accessor.Name); bool flag = !accessor.Mapping.TypeDesc.IsArray && (str == accessor.Mapping.TypeName); bool flag2 = mapping.TypeDesc == elementTypeDesc; bool flag3 = (accessor.Form == XmlSchemaForm.Unqualified) || (accessor.Namespace == ns); bool flag4 = accessor.IsNullable == mapping.TypeDesc.IsNullable; bool flag5 = accessor.Form != XmlSchemaForm.Unqualified; if (((!flag || !flag2) || (!flag3 || !flag4)) || (!flag5 || (nestingLevel > 0))) { this.ExportArrayItem(metadata, flag ? null : str, flag3 ? null : accessor.Namespace, flag2 ? null : mapping.TypeDesc, mapping.TypeDesc, accessor.IsNullable, flag5 ? XmlSchemaForm.None : accessor.Form, nestingLevel); } if (mapping is ArrayMapping) { this.ExportArrayElements(metadata, (ArrayMapping)mapping, ns, elementTypeDesc.ArrayElementTypeDesc, nestingLevel + 1); } } }
void ExportElement(ElementAccessor element) { ExportType(element.Mapping, Accessor.UnescapeName(element.Name), element.Namespace, element); }
internal string GenerateTypedSerializer(string readMethod, string writeMethod, XmlMapping mapping, CodeIdentifiers classes, string baseSerializer, string readerClass, string writerClass) { string serializerName = CodeIdentifier.MakeValid(Accessor.UnescapeName(mapping.Accessor.Mapping.TypeDesc.Name)); serializerName = classes.AddUnique(serializerName + "Serializer", mapping); TypeBuilder typedSerializerTypeBuilder = CodeGenerator.CreateTypeBuilder( _moduleBuilder, CodeIdentifier.GetCSharpName(serializerName), TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit, CreatedTypes[baseSerializer], Array.Empty <Type>() ); ilg = new CodeGenerator(typedSerializerTypeBuilder); ilg.BeginMethod( typeof(Boolean), "CanDeserialize", new Type[] { typeof(XmlReader) }, new string[] { "xmlReader" }, CodeGenerator.PublicOverrideMethodAttributes ); if (mapping.Accessor.Any) { ilg.Ldc(true); ilg.Stloc(ilg.ReturnLocal); ilg.Br(ilg.ReturnLabel); } else { MethodInfo XmlReader_IsStartElement = typeof(XmlReader).GetMethod( "IsStartElement", CodeGenerator.InstanceBindingFlags, new Type[] { typeof(String), typeof(String) } ); ilg.Ldarg(ilg.GetArg("xmlReader")); ilg.Ldstr(GetCSharpString(mapping.Accessor.Name)); ilg.Ldstr(GetCSharpString(mapping.Accessor.Namespace)); ilg.Call(XmlReader_IsStartElement); ilg.Stloc(ilg.ReturnLocal); ilg.Br(ilg.ReturnLabel); } ilg.MarkLabel(ilg.ReturnLabel); ilg.Ldloc(ilg.ReturnLocal); ilg.EndMethod(); if (writeMethod != null) { ilg = new CodeGenerator(typedSerializerTypeBuilder); ilg.BeginMethod( typeof(void), "Serialize", new Type[] { typeof(object), typeof(XmlSerializationWriter) }, new string[] { "objectToSerialize", "writer" }, CodeGenerator.ProtectedOverrideMethodAttributes); MethodInfo writerType_writeMethod = CreatedTypes[writerClass].GetMethod( writeMethod, CodeGenerator.InstanceBindingFlags, new Type[] { (mapping is XmlMembersMapping) ? typeof(object[]) : typeof(object) } ); ilg.Ldarg("writer"); ilg.Castclass(CreatedTypes[writerClass]); ilg.Ldarg("objectToSerialize"); if (mapping is XmlMembersMapping) { ilg.ConvertValue(typeof(object), typeof(object[])); } ilg.Call(writerType_writeMethod); ilg.EndMethod(); } if (readMethod != null) { ilg = new CodeGenerator(typedSerializerTypeBuilder); ilg.BeginMethod( typeof(object), "Deserialize", new Type[] { typeof(XmlSerializationReader) }, new string[] { "reader" }, CodeGenerator.ProtectedOverrideMethodAttributes); MethodInfo readerType_readMethod = CreatedTypes[readerClass].GetMethod( readMethod, CodeGenerator.InstanceBindingFlags, Array.Empty <Type>() ); ilg.Ldarg("reader"); ilg.Castclass(CreatedTypes[readerClass]); ilg.Call(readerType_readMethod); ilg.EndMethod(); } typedSerializerTypeBuilder.DefineDefaultConstructor(CodeGenerator.PublicMethodAttributes); Type typedSerializerType = typedSerializerTypeBuilder.CreateTypeInfo().AsType(); CreatedTypes.Add(typedSerializerType.Name, typedSerializerType); return(typedSerializerType.Name); }
internal string GenerateTypedSerializer(string readMethod, string writeMethod, XmlMapping mapping, CodeIdentifiers classes, string baseSerializer, string readerClass, string writerClass) { string serializerName = CodeIdentifier.MakeValid(Accessor.UnescapeName(mapping.Accessor.Mapping.TypeDesc.Name)); serializerName = classes.AddUnique(serializerName + "Serializer", mapping); _writer.WriteLine(); _writer.Write("public sealed class "); _writer.Write(CodeIdentifier.GetCSharpName(serializerName)); _writer.Write(" : "); _writer.Write(baseSerializer); _writer.WriteLine(" {"); _writer.Indent++; _writer.WriteLine(); _writer.Write("public override "); _writer.Write(typeof(bool).FullName); _writer.Write(" CanDeserialize("); _writer.Write(typeof(XmlReader).FullName); _writer.WriteLine(" xmlReader) {"); _writer.Indent++; if (mapping.Accessor.Any) { _writer.WriteLine("return true;"); } else { _writer.Write("return xmlReader.IsStartElement("); WriteQuotedCSharpString(mapping.Accessor.Name); _writer.Write(", "); WriteQuotedCSharpString(mapping.Accessor.Namespace); _writer.WriteLine(");"); } _writer.Indent--; _writer.WriteLine("}"); if (writeMethod != null) { _writer.WriteLine(); _writer.Write("protected override void Serialize(object objectToSerialize, "); _writer.Write(typeof(System.Xml.Serialization.XmlSerializationWriter).FullName); _writer.WriteLine(" writer) {"); _writer.Indent++; _writer.Write("(("); _writer.Write(writerClass); _writer.Write(")writer)."); _writer.Write(writeMethod); _writer.Write("("); if (mapping is XmlMembersMapping) { _writer.Write("(object[])"); } _writer.WriteLine("objectToSerialize);"); _writer.Indent--; _writer.WriteLine("}"); } if (readMethod != null) { _writer.WriteLine(); _writer.Write("protected override object Deserialize("); _writer.Write(typeof(System.Xml.Serialization.XmlSerializationReader).FullName); _writer.WriteLine(" reader) {"); _writer.Indent++; _writer.Write("return (("); _writer.Write(readerClass); _writer.Write(")reader)."); _writer.Write(readMethod); _writer.WriteLine("();"); _writer.Indent--; _writer.WriteLine("}"); } _writer.Indent--; _writer.WriteLine("}"); return(serializerName); }
private TypeMapping ImportEnumeratedDataType(XmlSchemaSimpleType dataType, string typeNs, string identifier, bool isList) { TypeMapping mapping = (TypeMapping)ImportedMappings[dataType]; if (mapping != null) { return(mapping); } XmlSchemaSimpleType sourceDataType = FindDataType(dataType.DerivedFrom); TypeDesc sourceTypeDesc = Scope.GetTypeDesc(sourceDataType); if (sourceTypeDesc != null && sourceTypeDesc != Scope.GetTypeDesc(typeof(string))) { return(ImportPrimitiveDataType(dataType)); } identifier = Accessor.UnescapeName(identifier); string typeName = GenerateUniqueTypeName(identifier); EnumMapping enumMapping = new EnumMapping(); enumMapping.IsReference = Schemas.IsReference(dataType); enumMapping.TypeDesc = new TypeDesc(typeName, typeName, TypeKind.Enum, null, 0); enumMapping.TypeName = identifier; enumMapping.Namespace = typeNs; enumMapping.IsFlags = isList; CodeIdentifiers constants = new CodeIdentifiers(); if (!(dataType.Content is XmlSchemaSimpleTypeRestriction)) { throw new InvalidOperationException(SR.Format(SR.XmlInvalidEnumContent, dataType.Content.GetType().Name, identifier)); } XmlSchemaSimpleTypeRestriction restriction = (XmlSchemaSimpleTypeRestriction)dataType.Content; for (int i = 0; i < restriction.Facets.Count; i++) { object facet = restriction.Facets[i]; if (!(facet is XmlSchemaEnumerationFacet)) { continue; } XmlSchemaEnumerationFacet enumeration = (XmlSchemaEnumerationFacet)facet; ConstantMapping constant = new ConstantMapping(); string constantName = CodeIdentifier.MakeValid(enumeration.Value); constant.Name = constants.AddUnique(constantName, constant); constant.XmlName = enumeration.Value; constant.Value = i; } enumMapping.Constants = (ConstantMapping[])constants.ToArray(typeof(ConstantMapping)); if (isList && enumMapping.Constants.Length > 63) { // if we have 64+ flag constants we cannot map the type to long enum, we will use string mapping instead. mapping = new PrimitiveMapping(); mapping.TypeDesc = Scope.GetTypeDesc(typeof(string)); mapping.TypeName = mapping.TypeDesc.DataType.Name; ImportedMappings.Add(dataType, mapping); return(mapping); } ImportedMappings.Add(dataType, enumMapping); Scope.AddTypeMapping(enumMapping); return(enumMapping); }
private StructMapping ImportStructType(XmlSchemaComplexType type, string typeNs, bool excludeFromImport) { if (type.Name == null) { XmlSchemaElement element = (XmlSchemaElement)type.Parent; XmlQualifiedName parentType = XmlSchemas.GetParentName(element); throw new InvalidOperationException(SR.Format(SR.XmlInvalidSchemaElementType, parentType.Name, parentType.Namespace, element.Name)); } TypeDesc baseTypeDesc = null; Mapping baseMapping = null; if (!type.DerivedFrom.IsEmpty) { baseMapping = ImportType(type.DerivedFrom, excludeFromImport); if (baseMapping is StructMapping) { baseTypeDesc = ((StructMapping)baseMapping).TypeDesc; } else { baseMapping = null; } } if (baseMapping == null) { baseMapping = GetRootMapping(); } Mapping previousMapping = (Mapping)ImportedMappings[type]; if (previousMapping != null) { return((StructMapping)previousMapping); } string typeName = GenerateUniqueTypeName(Accessor.UnescapeName(type.Name)); StructMapping structMapping = new StructMapping(); structMapping.IsReference = Schemas.IsReference(type); TypeFlags flags = TypeFlags.Reference; if (type.IsAbstract) { flags |= TypeFlags.Abstract; } structMapping.TypeDesc = new TypeDesc(typeName, typeName, TypeKind.Struct, baseTypeDesc, flags); structMapping.Namespace = typeNs; structMapping.TypeName = type.Name; structMapping.BaseMapping = (StructMapping)baseMapping; ImportedMappings.Add(type, structMapping); if (excludeFromImport) { structMapping.IncludeInSchema = false; } CodeIdentifiers members = new CodeIdentifiers(); members.AddReserved(typeName); AddReservedIdentifiersForDataBinding(members); structMapping.Members = ImportTypeMembers(type, typeNs, members); Scope.AddTypeMapping(structMapping); ImportDerivedTypes(new XmlQualifiedName(type.Name, typeNs)); return(structMapping); }
void ExportType(TypeMapping mapping) { if (mapping.IsReference) { return; } if (ExportedMappings[mapping] == null) { CodeTypeDeclaration codeClass = null; ExportedMappings.Add(mapping, mapping); if (mapping is EnumMapping) { codeClass = ExportEnum((EnumMapping)mapping, typeof(SoapEnumAttribute)); } else if (mapping is StructMapping) { codeClass = ExportStruct((StructMapping)mapping); } else if (mapping is ArrayMapping) { EnsureTypesExported(((ArrayMapping)mapping).Elements, null); } if (codeClass != null) { // Add [GeneratedCodeAttribute(Tool=.., Version=..)] codeClass.CustomAttributes.Add(GeneratedCodeAttribute); // Add [SerializableAttribute] codeClass.CustomAttributes.Add(new CodeAttributeDeclaration(typeof(SerializableAttribute).FullName)); if (!codeClass.IsEnum) { // Add [DebuggerStepThrough] codeClass.CustomAttributes.Add(new CodeAttributeDeclaration(typeof(DebuggerStepThroughAttribute).FullName)); // Add [DesignerCategory("code")] codeClass.CustomAttributes.Add(new CodeAttributeDeclaration(typeof(DesignerCategoryAttribute).FullName, new CodeAttributeArgument[] { new CodeAttributeArgument(new CodePrimitiveExpression("code")) })); } AddTypeMetadata(codeClass.CustomAttributes, typeof(SoapTypeAttribute), mapping.TypeDesc.Name, Accessor.UnescapeName(mapping.TypeName), mapping.Namespace, mapping.IncludeInSchema); ExportedClasses.Add(mapping, codeClass); } } }
private void AddMemberMetadata(CodeMemberField field, CodeAttributeDeclarationCollection metadata, MemberMapping member, string ns, bool forceUseMemberName, CodeCommentStatementCollection comments, CodeConstructor ctor) { if (member.Xmlns != null) { CodeAttributeDeclaration declaration = new CodeAttributeDeclaration(typeof(XmlNamespaceDeclarationsAttribute).FullName); metadata.Add(declaration); } else if (member.Attribute != null) { AttributeAccessor attribute = member.Attribute; if (attribute.Any) { this.ExportAnyAttribute(metadata); } else { TypeMapping mapping = attribute.Mapping; string str = Accessor.UnescapeName(attribute.Name); bool flag = (mapping.TypeDesc == member.TypeDesc) || (member.TypeDesc.IsArrayLike && (mapping.TypeDesc == member.TypeDesc.ArrayElementTypeDesc)); bool flag2 = (str == member.Name) && !forceUseMemberName; bool flag3 = attribute.Namespace == ns; bool flag4 = attribute.Form != XmlSchemaForm.Qualified; this.ExportAttribute(metadata, flag2 ? null : str, (flag3 || flag4) ? null : attribute.Namespace, flag ? null : mapping.TypeDesc, mapping.TypeDesc, flag4 ? XmlSchemaForm.None : attribute.Form); this.AddDefaultValueAttribute(field, metadata, attribute.Default, mapping, comments, member.TypeDesc, attribute, ctor); } } else { if (member.Text != null) { TypeMapping mapping2 = member.Text.Mapping; bool flag5 = (mapping2.TypeDesc == member.TypeDesc) || (member.TypeDesc.IsArrayLike && (mapping2.TypeDesc == member.TypeDesc.ArrayElementTypeDesc)); this.ExportText(metadata, flag5 ? null : mapping2.TypeDesc, mapping2.TypeDesc.IsAmbiguousDataType ? mapping2.TypeDesc.DataType.Name : null); } if (member.Elements.Length == 1) { ElementAccessor accessor = member.Elements[0]; TypeMapping mapping3 = accessor.Mapping; string name = Accessor.UnescapeName(accessor.Name); bool flag6 = (name == member.Name) && !forceUseMemberName; bool flag7 = mapping3 is ArrayMapping; bool flag8 = accessor.Namespace == ns; bool flag9 = accessor.Form != XmlSchemaForm.Unqualified; if (accessor.Any) { this.ExportAnyElement(metadata, name, accessor.Namespace, member.SequenceId); } else if (flag7) { TypeDesc typeDesc = mapping3.TypeDesc; TypeDesc desc2 = member.TypeDesc; ArrayMapping array = (ArrayMapping)mapping3; if ((!flag6 || !flag8) || ((accessor.IsNullable || !flag9) || (member.SequenceId != -1))) { this.ExportArray(metadata, flag6 ? null : name, flag8 ? null : accessor.Namespace, accessor.IsNullable, flag9 ? XmlSchemaForm.None : accessor.Form, member.SequenceId); } else if (mapping3.TypeDesc.ArrayElementTypeDesc == new TypeScope().GetTypeDesc(typeof(byte))) { this.ExportArray(metadata, null, null, false, XmlSchemaForm.None, member.SequenceId); } this.ExportArrayElements(metadata, array, accessor.Namespace, member.TypeDesc.ArrayElementTypeDesc, 0); } else { bool flag10 = (mapping3.TypeDesc == member.TypeDesc) || (member.TypeDesc.IsArrayLike && (mapping3.TypeDesc == member.TypeDesc.ArrayElementTypeDesc)); if (member.TypeDesc.IsArrayLike) { flag6 = false; } this.ExportElement(metadata, flag6 ? null : name, flag8 ? null : accessor.Namespace, flag10 ? null : mapping3.TypeDesc, mapping3.TypeDesc, accessor.IsNullable, flag9 ? XmlSchemaForm.None : accessor.Form, member.SequenceId); } this.AddDefaultValueAttribute(field, metadata, accessor.Default, mapping3, comments, member.TypeDesc, accessor, ctor); } else { for (int i = 0; i < member.Elements.Length; i++) { ElementAccessor accessor3 = member.Elements[i]; string str3 = Accessor.UnescapeName(accessor3.Name); bool flag11 = accessor3.Namespace == ns; if (accessor3.Any) { this.ExportAnyElement(metadata, str3, accessor3.Namespace, member.SequenceId); } else { bool flag12 = accessor3.Form != XmlSchemaForm.Unqualified; this.ExportElement(metadata, str3, flag11 ? null : accessor3.Namespace, accessor3.Mapping.TypeDesc, accessor3.Mapping.TypeDesc, accessor3.IsNullable, flag12 ? XmlSchemaForm.None : accessor3.Form, member.SequenceId); } } } if (member.ChoiceIdentifier != null) { CodeAttributeDeclaration declaration2 = new CodeAttributeDeclaration(typeof(XmlChoiceIdentifierAttribute).FullName); declaration2.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(member.ChoiceIdentifier.MemberName))); metadata.Add(declaration2); } if (member.Ignore) { CodeAttributeDeclaration declaration3 = new CodeAttributeDeclaration(typeof(XmlIgnoreAttribute).FullName); metadata.Add(declaration3); } } }
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); } } }
private void ExportType(TypeMapping mapping, string name, string ns, ElementAccessor rootElement, bool checkReference) { if ((!mapping.IsReference || (mapping.Namespace == "http://schemas.xmlsoap.org/soap/encoding/")) && ((!(mapping is StructMapping) || !checkReference) || (!((StructMapping)mapping).ReferencedByTopLevelElement || (rootElement != null)))) { if (((mapping is ArrayMapping) && (rootElement != null)) && (rootElement.IsTopLevelInSchema && (((ArrayMapping)mapping).TopLevelMapping != null))) { mapping = ((ArrayMapping)mapping).TopLevelMapping; } CodeTypeDeclaration declaration = null; if (base.ExportedMappings[mapping] == null) { base.ExportedMappings.Add(mapping, mapping); if (mapping.TypeDesc.IsMappedType) { declaration = mapping.TypeDesc.ExtendedType.ExportTypeDefinition(base.CodeNamespace, base.CodeCompileUnit); } else if (mapping is EnumMapping) { declaration = base.ExportEnum((EnumMapping)mapping, typeof(XmlEnumAttribute)); } else if (mapping is StructMapping) { declaration = this.ExportStruct((StructMapping)mapping); } else if (mapping is ArrayMapping) { this.EnsureTypesExported(((ArrayMapping)mapping).Elements, ns); } if (declaration != null) { if (!mapping.TypeDesc.IsMappedType) { declaration.CustomAttributes.Add(base.GeneratedCodeAttribute); declaration.CustomAttributes.Add(new CodeAttributeDeclaration(typeof(SerializableAttribute).FullName)); if (!declaration.IsEnum) { declaration.CustomAttributes.Add(new CodeAttributeDeclaration(typeof(DebuggerStepThroughAttribute).FullName)); declaration.CustomAttributes.Add(new CodeAttributeDeclaration(typeof(DesignerCategoryAttribute).FullName, new CodeAttributeArgument[] { new CodeAttributeArgument(new CodePrimitiveExpression("code")) })); } base.AddTypeMetadata(declaration.CustomAttributes, typeof(XmlTypeAttribute), mapping.TypeDesc.Name, Accessor.UnescapeName(mapping.TypeName), mapping.Namespace, mapping.IncludeInSchema); } else if (CodeExporter.FindAttributeDeclaration(typeof(GeneratedCodeAttribute), declaration.CustomAttributes) == null) { declaration.CustomAttributes.Add(base.GeneratedCodeAttribute); } base.ExportedClasses.Add(mapping, declaration); } } else { declaration = (CodeTypeDeclaration)base.ExportedClasses[mapping]; } if ((declaration != null) && (rootElement != null)) { this.AddRootMetadata(declaration.CustomAttributes, mapping, name, ns, rootElement); } } }
private void ExportElement(ElementAccessor element) { this.ExportType(element.Mapping, Accessor.UnescapeName(element.Name), element.Namespace, element, true); }
private StructMapping ImportStructType(XmlSchemaComplexType type, string typeNs, bool excludeFromImport) { if (type.Name == null) { XmlSchemaElement parent = (XmlSchemaElement)type.Parent; XmlQualifiedName parentName = XmlSchemas.GetParentName(parent); throw new InvalidOperationException(Res.GetString("XmlInvalidSchemaElementType", new object[] { parentName.Name, parentName.Namespace, parent.Name })); } TypeDesc baseTypeDesc = null; Mapping rootMapping = null; if (!type.DerivedFrom.IsEmpty) { rootMapping = this.ImportType(type.DerivedFrom, excludeFromImport); if (rootMapping is StructMapping) { baseTypeDesc = ((StructMapping)rootMapping).TypeDesc; } else { rootMapping = null; } } if (rootMapping == null) { rootMapping = base.GetRootMapping(); } Mapping mapping2 = (Mapping)base.ImportedMappings[type]; if (mapping2 != null) { return((StructMapping)mapping2); } string str = base.GenerateUniqueTypeName(Accessor.UnescapeName(type.Name)); StructMapping mapping3 = new StructMapping { IsReference = base.Schemas.IsReference(type) }; TypeFlags reference = TypeFlags.Reference; if (type.IsAbstract) { reference |= TypeFlags.Abstract; } mapping3.TypeDesc = new TypeDesc(str, str, TypeKind.Struct, baseTypeDesc, reference); mapping3.Namespace = typeNs; mapping3.TypeName = type.Name; mapping3.BaseMapping = (StructMapping)rootMapping; base.ImportedMappings.Add(type, mapping3); if (excludeFromImport) { mapping3.IncludeInSchema = false; } CodeIdentifiers scope = new CodeIdentifiers(); scope.AddReserved(str); base.AddReservedIdentifiersForDataBinding(scope); mapping3.Members = this.ImportTypeMembers(type, typeNs, scope); base.Scope.AddTypeMapping(mapping3); this.ImportDerivedTypes(new XmlQualifiedName(type.Name, typeNs)); return(mapping3); }
private void ExportType(TypeMapping mapping) { if (!mapping.IsReference && (base.ExportedMappings[mapping] == null)) { CodeTypeDeclaration declaration = null; base.ExportedMappings.Add(mapping, mapping); if (mapping is EnumMapping) { declaration = base.ExportEnum((EnumMapping)mapping, typeof(SoapEnumAttribute)); } else if (mapping is StructMapping) { declaration = this.ExportStruct((StructMapping)mapping); } else if (mapping is ArrayMapping) { this.EnsureTypesExported(((ArrayMapping)mapping).Elements, null); } if (declaration != null) { declaration.CustomAttributes.Add(base.GeneratedCodeAttribute); declaration.CustomAttributes.Add(new CodeAttributeDeclaration(typeof(SerializableAttribute).FullName)); if (!declaration.IsEnum) { declaration.CustomAttributes.Add(new CodeAttributeDeclaration(typeof(DebuggerStepThroughAttribute).FullName)); declaration.CustomAttributes.Add(new CodeAttributeDeclaration(typeof(DesignerCategoryAttribute).FullName, new CodeAttributeArgument[] { new CodeAttributeArgument(new CodePrimitiveExpression("code")) })); } base.AddTypeMetadata(declaration.CustomAttributes, typeof(SoapTypeAttribute), mapping.TypeDesc.Name, Accessor.UnescapeName(mapping.TypeName), mapping.Namespace, mapping.IncludeInSchema); base.ExportedClasses.Add(mapping, declaration); } } }