public void PrimitiveType() { XsdDataContractExporter xdce = new XsdDataContractExporter(); Assert.AreEqual(1, xdce.Schemas.Count); Assert.IsNull(xdce.GetSchemaType(typeof(int))); Assert.AreEqual(new QName("int", XmlSchema.Namespace), xdce.GetSchemaTypeName(typeof(int))); xdce.Export(typeof(int)); Assert.IsNull(xdce.GetSchemaType(typeof(int))); Assert.AreEqual(new QName("int", XmlSchema.Namespace), xdce.GetSchemaTypeName(typeof(int))); }
/// <summary> /// Returns the sequence of properties of the specified type. /// </summary> /// <param name="type">The type.</param> /// <param name="schema">The schema.</param> /// <param name="schemaSet">The schema set.</param> /// <param name="serializationManager">The serialization manager.</param> /// <param name="exportedTypes">The exported types.</param> /// <returns>Sequence containing all properties.</returns> private static XmlSchemaSequence GetPropertiesSequence(Type type, XmlSchema schema, XmlSchemaSet schemaSet, ISerializationManager serializationManager, HashSet <string> exportedTypes) { Argument.IsNotNull("type", type); Argument.IsNotNull("schema", schema); Argument.IsNotNull("schemaSet", schemaSet); var propertiesSequence = new XmlSchemaSequence(); if (type.IsModelBase()) { var members = new List <MemberMetadata>(); members.AddRange(from field in serializationManager.GetFieldsToSerialize(type) select field.Value); members.AddRange(from property in serializationManager.GetCatelPropertiesToSerialize(type) select property.Value); members.AddRange(from property in serializationManager.GetRegularPropertiesToSerialize(type) select property.Value); foreach (var member in members) { var propertySchemaElement = new XmlSchemaElement(); propertySchemaElement.Name = member.MemberName; var memberType = member.MemberType; propertySchemaElement.IsNillable = memberType.IsNullableType(); propertySchemaElement.MinOccurs = 0; var exporter = new XsdDataContractExporter(schemaSet); var alreadyExported = IsAlreadyExported(schemaSet, memberType, exporter, exportedTypes); if (!alreadyExported) { if (!exportedTypes.Contains(memberType.FullName)) { exportedTypes.Add(memberType.FullName); } try { if (exporter.CanExport(memberType)) { exporter.Export(memberType); } } catch (Exception) { // Ignore } } propertySchemaElement.SchemaType = exporter.GetSchemaType(memberType); propertySchemaElement.SchemaTypeName = exporter.GetSchemaTypeName(memberType); propertiesSequence.Items.Add(propertySchemaElement); } } return(propertiesSequence); }
public void GetSchemaTypeTest() { XsdDataContractExporter xdce = new XsdDataContractExporter(); Assert.IsNull(xdce.GetSchemaType(typeof(dc))); Assert.AreEqual(new QName("_dc", "http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"), xdce.GetSchemaTypeName(typeof(dc))); }
public void GetSchemaType(string testname, Type t, XmlSchemaType stName, Type expectedExceptionType = null, string msg = null) { _output.WriteLine($"=============== {testname} ==============="); XsdDataContractExporter exporter = new XsdDataContractExporter(); if (expectedExceptionType == null) { XmlSchemaType schemaType = exporter.GetSchemaType(t); Assert.Equal(stName, schemaType); } else { var ex = Assert.Throws(expectedExceptionType, () => exporter.GetSchemaType(t)); if (!string.IsNullOrEmpty(msg)) { Assert.Equal(msg, ex.Message); } } }
/// <summary> /// Returns the sequence of properties of the specified type. /// </summary> /// <param name="type">The type.</param> /// <param name="schema">The schema.</param> /// <param name="schemaSet">The schema set.</param> /// <param name="serializationManager">The serialization manager.</param> /// <returns>Sequence containing all properties.</returns> private static XmlSchemaSequence GetPropertiesSequence(Type type, XmlSchema schema, XmlSchemaSet schemaSet, ISerializationManager serializationManager) { Argument.IsNotNull("type", type); Argument.IsNotNull("schema", schema); Argument.IsNotNull("schemaSet", schemaSet); var propertiesSequence = new XmlSchemaSequence(); if (typeof(ModelBase).IsAssignableFromEx(type)) { var members = new List <MemberInfo>(); members.AddRange(from field in serializationManager.GetFieldsToSerialize(type) select type.GetFieldEx(field)); members.AddRange(from property in serializationManager.GetPropertiesToSerialize(type) select type.GetPropertyEx(property)); foreach (var member in members) { var propertySchemaElement = new XmlSchemaElement(); propertySchemaElement.Name = member.Name; var memberType = typeof(object); var fieldInfo = member as FieldInfo; if (fieldInfo != null) { memberType = fieldInfo.FieldType; } var propertyInfo = member as PropertyInfo; if (propertyInfo != null) { memberType = propertyInfo.PropertyType; } propertySchemaElement.IsNillable = memberType.IsNullableType(); propertySchemaElement.MinOccurs = 0; var exporter = new XsdDataContractExporter(schemaSet); exporter.Export(memberType); propertySchemaElement.SchemaType = exporter.GetSchemaType(memberType); propertySchemaElement.SchemaTypeName = exporter.GetSchemaTypeName(memberType); propertiesSequence.Items.Add(propertySchemaElement); } } return(propertiesSequence); }