MessagePartDescription CreateMessagePart (XmlSchemaElement elem) { var part = new MessagePartDescription (elem.QualifiedName.Name, elem.QualifiedName.Namespace); part.Importer = dc_importer; var typeQName = dc_importer.Import (schema_set_in_use, elem); part.CodeTypeReference = dc_importer.GetCodeTypeReference (typeQName); return part; }
private void MapTypes(CodeCompileUnit compileUnit) { foreach (var complexType in _serviceModel.ComplexTypes) { var typeReference = _xsdDataContractImporter.GetCodeTypeReference(complexType.QualifiedName); var typeDeclaration = compileUnit.FindTypeDeclaration(typeReference.BaseType); // skip complex type for which no type was generated (eg. a List<>) if (typeDeclaration == null) { continue; } foreach (var property in typeDeclaration.Properties()) { var element = complexType.Elements.SingleOrDefault(p => p.Name == property.Name); if (element == null) { throw new Exception(); } if (element.MaxOccurs != 1) { continue; } if (!_xmlTypeMapping.TryGetValue(element.TypeCode, out var elementTypeMapping)) { continue; } if (element.MinOccurs == 0) { property.Type = elementTypeMapping.CodeTypeReference.ToNullable(); } else { property.Type = elementTypeMapping.CodeTypeReference; } if (property.GetStatements[0] is CodeMethodReturnStatement returnStatement && returnStatement.Expression != null) { if (returnStatement.Expression is CodeFieldReferenceExpression fieldReference) { var field = typeDeclaration.Fields().SingleOrDefault(p => p.Name == fieldReference.FieldName); if (field == null) { throw new Exception(); } field.Type = property.Type; } } } } }
MessagePartDescription CreateMessagePart(XmlSchemaElement elem, Message msg, MessagePart msgPart) { var part = new MessagePartDescription(elem.QualifiedName.Name, elem.QualifiedName.Namespace); part.DataContractImporter = dc_importer; if (dc_importer.CanImport(schema_set_in_use, elem)) { var typeQName = dc_importer.Import(schema_set_in_use, elem); part.CodeTypeReference = dc_importer.GetCodeTypeReference(elem.ElementSchemaType.QualifiedName, elem); } return(part); }
public void GetCodeTypeReferenceTest2 () { NewXmlSchemaSet (); Assert.IsFalse (xss.IsCompiled, "#g01"); XsdDataContractImporter xsdi = GetImporter (); xsdi.Import (xss, new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/")); Assert.IsTrue (xss.IsCompiled, "#g02"); CodeTypeReference type = xsdi.GetCodeTypeReference (new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/")); //FIXME: How should this type ref be checked? Assert.IsNotNull (type, "#g03"); Assert.AreEqual (type.BaseType, "dc", "#g04"); }
private void MarkTypesAbstract(CodeCompileUnit compileUnit) { foreach (var complexType in _serviceModel.ComplexTypes) { if (!complexType.IsAbstract) { continue; } var typeReference = _xsdDataContractImporter.GetCodeTypeReference(complexType.QualifiedName); CodeTypeName typeName = typeReference.BaseType; var typeDeclaration = typeName.Find(compileUnit); if (typeDeclaration == null) { throw new Exception($"Declaration for type '{typeName}' not found in compile unit."); } typeDeclaration.TypeAttributes |= TypeAttributes.Abstract; } }
/// <summary> /// For those properties for which <see cref="DataMemberAttribute.IsRequired"/> is <c>true</c>, set <see cref="DataMemberAttribute.EmitDefaultValue"/> /// to <c>true</c>; otherwise, set <see cref="DataMemberAttribute.EmitDefaultValue"/> to <c>false</c>. /// </summary> /// <param name="compileUnit">The compile unit.</param> private void FixEmitDefaultValue(CodeCompileUnit compileUnit) { foreach (var complexType in _serviceModel.ComplexTypes) { var typeReference = _xsdDataContractImporter.GetCodeTypeReference(complexType.QualifiedName); var typeDeclaration = compileUnit.FindTypeDeclaration(typeReference.BaseType); // skip complex type for which no type was generated (eg. a List<>) if (typeDeclaration == null) { continue; } foreach (var property in typeDeclaration.Properties()) { var dataMemberAttribute = property.CustomAttributes.SingleOrDefault <CodeAttributeDeclaration>(p => p.Name == typeof(DataMemberAttribute).FullName); if (dataMemberAttribute == null) { continue; } var emitDefaultValue = IsRequired(dataMemberAttribute) && !ConsiderDefaultValueForTypeAsNoValue(compileUnit, _xmlTypeMappings, property.Type.BaseType); var emitDefaultValueArgument = dataMemberAttribute.Arguments.FindArgumentByName(nameof(DataMemberAttribute.EmitDefaultValue)); if (emitDefaultValueArgument == null) { emitDefaultValueArgument = new CodeAttributeArgument("EmitDefaultValue", new CodePrimitiveExpression(emitDefaultValue)); dataMemberAttribute.Arguments.Add(emitDefaultValueArgument); } else { emitDefaultValueArgument.Value = new CodePrimitiveExpression(emitDefaultValue); } } } }
public void GetCodeTypeReferenceTest() { XsdDataContractImporter xsdi = GetImporter(); xsdi.GetCodeTypeReference(new XmlQualifiedName("dc", "http://schemas.datacontract.org/2004/07/")); }