internal override void EnsureTypesExported(Accessor[] accessors, string ns) { if (accessors != null) { for (int i = 0; i < accessors.Length; i++) { this.ExportType(accessors[i].Mapping); } } }
private static void AddUniqueAccessor(INameScope scope, Accessor accessor) { Accessor accessor2 = (Accessor) scope[accessor.Name, accessor.Namespace]; if (accessor2 != null) { if (accessor is ElementAccessor) { throw new InvalidOperationException(Res.GetString("XmlDuplicateElementName", new object[] { accessor2.Name, accessor2.Namespace })); } throw new InvalidOperationException(Res.GetString("XmlDuplicateAttributeName", new object[] { accessor2.Name, accessor2.Namespace })); } scope[accessor.Name, accessor.Namespace] = accessor; }
Accessor ReconcileAccessor(Accessor accessor, NameTable accessors) { if (accessor.Any && accessor.Name.Length == 0) return accessor; Accessor existing = (Accessor)accessors[accessor.Name, accessor.Namespace]; if (existing == null) { accessor.IsTopLevelInSchema = true; accessors.Add(accessor.Name, accessor.Namespace, accessor); return accessor; } if (existing.Mapping == accessor.Mapping) return existing; if (!(accessor.Mapping is MembersMapping) && !(existing.Mapping is MembersMapping)) { if (accessor.Mapping.TypeDesc == existing.Mapping.TypeDesc || (existing.Mapping is NullableMapping && accessor.Mapping.TypeDesc == ((NullableMapping)existing.Mapping).BaseMapping.TypeDesc) || (accessor.Mapping is NullableMapping && ((NullableMapping)accessor.Mapping).BaseMapping.TypeDesc == existing.Mapping.TypeDesc)) { // need to compare default values string value1 = Convert.ToString(accessor.Default, CultureInfo.InvariantCulture); string value2 = Convert.ToString(existing.Default, CultureInfo.InvariantCulture); if (value1 == value2) { return existing; } throw new InvalidOperationException(Res.GetString(Res.XmlCannotReconcileAccessorDefault, accessor.Name, accessor.Namespace, value1, value2)); } } if (accessor.Mapping is MembersMapping || existing.Mapping is MembersMapping) throw new InvalidOperationException(Res.GetString(Res.XmlMethodTypeNameConflict, accessor.Name, accessor.Namespace)); if (accessor.Mapping is ArrayMapping) { if (!(existing.Mapping is ArrayMapping)) { throw new InvalidOperationException(Res.GetString(Res.XmlCannotReconcileAccessor, accessor.Name, accessor.Namespace, GetMappingName(existing.Mapping), GetMappingName(accessor.Mapping))); } ArrayMapping mapping = (ArrayMapping)accessor.Mapping; ArrayMapping existingMapping = mapping.IsAnonymousType ? null : (ArrayMapping)types[existing.Mapping.TypeName, existing.Mapping.Namespace]; ArrayMapping first = existingMapping; while (existingMapping != null) { if (existingMapping == accessor.Mapping) return existing; existingMapping = existingMapping.Next; } mapping.Next = first; if (!mapping.IsAnonymousType) types[existing.Mapping.TypeName, existing.Mapping.Namespace] = mapping; return existing; } if (accessor is AttributeAccessor) throw new InvalidOperationException(Res.GetString(Res.XmlCannotReconcileAttributeAccessor, accessor.Name, accessor.Namespace, GetMappingName(existing.Mapping), GetMappingName(accessor.Mapping))); else throw new InvalidOperationException(Res.GetString(Res.XmlCannotReconcileAccessor, accessor.Name, accessor.Namespace, GetMappingName(existing.Mapping), GetMappingName(accessor.Mapping))); }
static void AddUniqueAccessor(INameScope scope, Accessor accessor) { Accessor existing = (Accessor)scope[accessor.Name, accessor.Namespace]; if (existing != null) { if (accessor is ElementAccessor) { throw new InvalidOperationException(Res.GetString(Res.XmlDuplicateElementName, existing.Name, existing.Namespace)); } else { #if DEBUG if (!(accessor is AttributeAccessor)) throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Bad accessor type " + accessor.GetType().FullName)); #endif throw new InvalidOperationException(Res.GetString(Res.XmlDuplicateAttributeName, existing.Name, existing.Namespace)); } } else { scope[accessor.Name, accessor.Namespace] = accessor; } }
void EnsureTypesExported(Accessor accessor, string ns) { if (accessor == null) return; ExportType(accessor.Mapping, ns); }
static void DropDefaultAttribute(Accessor accessor, CodeCommentStatementCollection comments, string type) { if (!accessor.IsFixed && accessor.IsOptional) { AddWarningComment(comments, Res.GetString(Res.XmlDropDefaultAttribute, type)); } }
void AddDefaultValueAttribute(CodeMemberField field, CodeAttributeDeclarationCollection metadata, object defaultValue, TypeMapping mapping, CodeCommentStatementCollection comments, TypeDesc memberTypeDesc, Accessor accessor, CodeConstructor ctor) { string attributeName = accessor.IsFixed ? "fixed" : "default"; if (!memberTypeDesc.HasDefaultSupport) { if (comments != null && defaultValue is string) { DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName); // do not generate intializers for the user prefered types if they do not have default capability AddWarningComment(comments, Res.GetString(Res.XmlDropAttributeValue, attributeName, mapping.TypeName, defaultValue.ToString())); } return; } if (memberTypeDesc.IsArrayLike && accessor is ElementAccessor) { if (comments != null && defaultValue is string) { DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName); // do not generate intializers for array-like types AddWarningComment(comments, Res.GetString(Res.XmlDropArrayAttributeValue, attributeName, defaultValue.ToString(), ((ElementAccessor)accessor).Name)); } return; } if (mapping.TypeDesc.IsMappedType && field != null && defaultValue is string) { SchemaImporterExtension extension = mapping.TypeDesc.ExtendedType.Extension; CodeExpression init = extension.ImportDefaultValue((string)defaultValue, mapping.TypeDesc.FullName); if (init != null) { if (ctor != null) { AddInitializationStatement(ctor, field, init); } else { field.InitExpression = extension.ImportDefaultValue((string)defaultValue, mapping.TypeDesc.FullName); } } if (comments != null) { DropDefaultAttribute(accessor, comments, mapping.TypeDesc.FullName); if (init == null) { AddWarningComment(comments, Res.GetString(Res.XmlNotKnownDefaultValue, extension.GetType().FullName, attributeName, (string)defaultValue, mapping.TypeName, mapping.Namespace)); } } return; } object value = null; if (defaultValue is string || defaultValue == null) { value = ImportDefault(mapping, (string)defaultValue); } if (value == null) return; if (!(mapping is PrimitiveMapping)) { DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName); AddWarningComment(comments, Res.GetString(Res.XmlDropNonPrimitiveAttributeValue, attributeName, defaultValue.ToString())); return; } PrimitiveMapping pm = (PrimitiveMapping)mapping; if (comments != null && !pm.TypeDesc.HasDefaultSupport && pm.TypeDesc.IsMappedType) { // do not generate intializers for the user prefered types if they do not have default capability DropDefaultAttribute(accessor, comments, pm.TypeDesc.FullName); return; } if (value == DBNull.Value) { if (comments != null) { AddWarningComment(comments, Res.GetString(Res.XmlDropAttributeValue, attributeName, pm.TypeName, defaultValue.ToString())); } return; } CodeAttributeArgument[] arguments = null; CodeExpression initExpression = null; if (pm.IsList) { #if DEBUG // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe if (value.GetType() != typeof(object[])) throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Default value for list should be object[], not " + value.GetType().Name)); #endif object[] vals = (object[])value; CodeExpression[] initializers = new CodeExpression[vals.Length]; for (int i = 0; i < vals.Length; i++) { GetDefaultValueArguments(pm, vals[i], out initializers[i]); } initExpression = new CodeArrayCreateExpression(field.Type, initializers); } else { arguments = GetDefaultValueArguments(pm, value, out initExpression); } if (field != null) { if (ctor != null) { AddInitializationStatement(ctor, field, initExpression); } else { field.InitExpression = initExpression; } } if (arguments != null && pm.TypeDesc.HasDefaultSupport && accessor.IsOptional && !accessor.IsFixed) { // Add [DefaultValueAttribute] CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(typeof(DefaultValueAttribute).FullName, arguments); metadata.Add(attribute); } else if (comments != null) { DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName); } }
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); } } }
private Accessor ReconcileAccessor(Accessor accessor, System.Xml.Serialization.NameTable accessors) { if (accessor.Any && (accessor.Name.Length == 0)) { return accessor; } Accessor accessor2 = (Accessor) accessors[accessor.Name, accessor.Namespace]; if (accessor2 == null) { accessor.IsTopLevelInSchema = true; accessors.Add(accessor.Name, accessor.Namespace, accessor); return accessor; } if (accessor2.Mapping == accessor.Mapping) { return accessor2; } if ((!(accessor.Mapping is MembersMapping) && !(accessor2.Mapping is MembersMapping)) && (((accessor.Mapping.TypeDesc == accessor2.Mapping.TypeDesc) || ((accessor2.Mapping is NullableMapping) && (accessor.Mapping.TypeDesc == ((NullableMapping) accessor2.Mapping).BaseMapping.TypeDesc))) || ((accessor.Mapping is NullableMapping) && (((NullableMapping) accessor.Mapping).BaseMapping.TypeDesc == accessor2.Mapping.TypeDesc)))) { string str = Convert.ToString(accessor.Default, CultureInfo.InvariantCulture); string str2 = Convert.ToString(accessor2.Default, CultureInfo.InvariantCulture); if (str != str2) { throw new InvalidOperationException(Res.GetString("XmlCannotReconcileAccessorDefault", new object[] { accessor.Name, accessor.Namespace, str, str2 })); } return accessor2; } if ((accessor.Mapping is MembersMapping) || (accessor2.Mapping is MembersMapping)) { throw new InvalidOperationException(Res.GetString("XmlMethodTypeNameConflict", new object[] { accessor.Name, accessor.Namespace })); } if (accessor.Mapping is ArrayMapping) { if (!(accessor2.Mapping is ArrayMapping)) { throw new InvalidOperationException(Res.GetString("XmlCannotReconcileAccessor", new object[] { accessor.Name, accessor.Namespace, GetMappingName(accessor2.Mapping), GetMappingName(accessor.Mapping) })); } ArrayMapping mapping = (ArrayMapping) accessor.Mapping; ArrayMapping next = mapping.IsAnonymousType ? null : ((ArrayMapping) this.types[accessor2.Mapping.TypeName, accessor2.Mapping.Namespace]); ArrayMapping mapping3 = next; while (next != null) { if (next == accessor.Mapping) { return accessor2; } next = next.Next; } mapping.Next = mapping3; if (!mapping.IsAnonymousType) { this.types[accessor2.Mapping.TypeName, accessor2.Mapping.Namespace] = mapping; } return accessor2; } if (accessor is AttributeAccessor) { throw new InvalidOperationException(Res.GetString("XmlCannotReconcileAttributeAccessor", new object[] { accessor.Name, accessor.Namespace, GetMappingName(accessor2.Mapping), GetMappingName(accessor.Mapping) })); } throw new InvalidOperationException(Res.GetString("XmlCannotReconcileAccessor", new object[] { accessor.Name, accessor.Namespace, GetMappingName(accessor2.Mapping), GetMappingName(accessor.Mapping) })); }
void EnsureTypesExported(Accessor[] accessors, string ns) { if (accessors == null) return; for (int i = 0; i < accessors.Length; i++) EnsureTypesExported(accessors[i], ns); }
private void AddDefaultValueAttribute(CodeMemberField field, CodeAttributeDeclarationCollection metadata, object defaultValue, TypeMapping mapping, CodeCommentStatementCollection comments, TypeDesc memberTypeDesc, Accessor accessor, CodeConstructor ctor) { string str = accessor.IsFixed ? "fixed" : "default"; if (!memberTypeDesc.HasDefaultSupport) { if ((comments != null) && (defaultValue is string)) { DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName); CodeExporter.AddWarningComment(comments, Res.GetString("XmlDropAttributeValue", new object[] { str, mapping.TypeName, defaultValue.ToString() })); } } else if (memberTypeDesc.IsArrayLike && (accessor is ElementAccessor)) { if ((comments != null) && (defaultValue is string)) { DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName); CodeExporter.AddWarningComment(comments, Res.GetString("XmlDropArrayAttributeValue", new object[] { str, defaultValue.ToString(), ((ElementAccessor) accessor).Name })); } } else if ((mapping.TypeDesc.IsMappedType && (field != null)) && (defaultValue is string)) { SchemaImporterExtension extension = mapping.TypeDesc.ExtendedType.Extension; CodeExpression init = extension.ImportDefaultValue((string) defaultValue, mapping.TypeDesc.FullName); if (init != null) { if (ctor != null) { AddInitializationStatement(ctor, field, init); } else { field.InitExpression = extension.ImportDefaultValue((string) defaultValue, mapping.TypeDesc.FullName); } } if (comments != null) { DropDefaultAttribute(accessor, comments, mapping.TypeDesc.FullName); if (init == null) { CodeExporter.AddWarningComment(comments, Res.GetString("XmlNotKnownDefaultValue", new object[] { extension.GetType().FullName, str, (string) defaultValue, mapping.TypeName, mapping.Namespace })); } } } else { object obj2 = null; if ((defaultValue is string) || (defaultValue == null)) { obj2 = this.ImportDefault(mapping, (string) defaultValue); } if (obj2 != null) { if (!(mapping is PrimitiveMapping)) { DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName); CodeExporter.AddWarningComment(comments, Res.GetString("XmlDropNonPrimitiveAttributeValue", new object[] { str, defaultValue.ToString() })); } else { PrimitiveMapping mapping2 = (PrimitiveMapping) mapping; if (((comments != null) && !mapping2.TypeDesc.HasDefaultSupport) && mapping2.TypeDesc.IsMappedType) { DropDefaultAttribute(accessor, comments, mapping2.TypeDesc.FullName); } else if (obj2 == DBNull.Value) { if (comments != null) { CodeExporter.AddWarningComment(comments, Res.GetString("XmlDropAttributeValue", new object[] { str, mapping2.TypeName, defaultValue.ToString() })); } } else { CodeAttributeArgument[] arguments = null; CodeExpression initExpression = null; if (mapping2.IsList) { object[] objArray = (object[]) obj2; CodeExpression[] initializers = new CodeExpression[objArray.Length]; for (int i = 0; i < objArray.Length; i++) { this.GetDefaultValueArguments(mapping2, objArray[i], out initializers[i]); } initExpression = new CodeArrayCreateExpression(field.Type, initializers); } else { arguments = this.GetDefaultValueArguments(mapping2, obj2, out initExpression); } if (field != null) { if (ctor != null) { AddInitializationStatement(ctor, field, initExpression); } else { field.InitExpression = initExpression; } } if (((arguments != null) && mapping2.TypeDesc.HasDefaultSupport) && (accessor.IsOptional && !accessor.IsFixed)) { CodeAttributeDeclaration declaration = new CodeAttributeDeclaration(typeof(DefaultValueAttribute).FullName, arguments); metadata.Add(declaration); } else if (comments != null) { DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName); } } } } } }
private void EnsureTypesExported(Accessor accessor, string ns) { if (accessor != null) { this.ExportType(accessor.Mapping, null, ns, null, false); } }
private static void DropDefaultAttribute(Accessor accessor, CodeCommentStatementCollection comments, string type) { if (!accessor.IsFixed && accessor.IsOptional) { CodeExporter.AddWarningComment(comments, Res.GetString("XmlDropDefaultAttribute", new object[] { type })); } }
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 override void EnsureTypesExported(Accessor[] accessors, string ns) { if (accessors == null) return; for (int i = 0; i < accessors.Length; i++) ExportType(accessors[i].Mapping); }
internal abstract void EnsureTypesExported(Accessor[] accessors, string ns);
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); }