From 61bffc51a21567a1d20bd5b6e70707ab14d5b9df Mon Sep 17 00:00:00 2001 From: Michael Ganss Date: Fri, 9 Apr 2021 16:37:29 +0200 Subject: [PATCH] Support duration (fixes #258) --- XmlSchemaClassGenerator/CodeUtilities.cs | 3 ++- XmlSchemaClassGenerator/TypeModel.cs | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/XmlSchemaClassGenerator/CodeUtilities.cs b/XmlSchemaClassGenerator/CodeUtilities.cs index 64fb3fe3..4af5c042 100644 --- a/XmlSchemaClassGenerator/CodeUtilities.cs +++ b/XmlSchemaClassGenerator/CodeUtilities.cs @@ -177,7 +177,7 @@ public static Type GetEffectiveType(this XmlSchemaDatatype type, GeneratorConfig var resultType = type.TypeCode switch { XmlTypeCode.AnyAtomicType => typeof(string),// union - XmlTypeCode.AnyUri or XmlTypeCode.Duration or XmlTypeCode.GDay or XmlTypeCode.GMonth or XmlTypeCode.GMonthDay or XmlTypeCode.GYear or XmlTypeCode.GYearMonth => typeof(string), + XmlTypeCode.AnyUri or XmlTypeCode.GDay or XmlTypeCode.GMonth or XmlTypeCode.GMonthDay or XmlTypeCode.GYear or XmlTypeCode.GYearMonth => typeof(string), XmlTypeCode.Time => typeof(DateTime), XmlTypeCode.Idref => typeof(string), XmlTypeCode.Integer or XmlTypeCode.NegativeInteger or XmlTypeCode.NonNegativeInteger or XmlTypeCode.NonPositiveInteger or XmlTypeCode.PositiveInteger => GetIntegerDerivedType(type, configuration, restrictions), @@ -373,6 +373,7 @@ public static KeyValuePair ParseNamespace(string nsArg, st ("System.ComponentModel.DataAnnotations", c => c.CompactTypeNames && (c.DataAnnotationMode != DataAnnotationMode.None || c.EntityFramework)), ("System.Diagnostics", c => c.CompactTypeNames && c.GenerateDebuggerStepThroughAttribute), ("System.Linq", c => c.EnableDataBinding), + ("System.Xml", c => c.CompactTypeNames), ("System.Xml.Schema", c => c.CompactTypeNames), ("System.Xml.Serialization", c => c.CompactTypeNames) }; diff --git a/XmlSchemaClassGenerator/TypeModel.cs b/XmlSchemaClassGenerator/TypeModel.cs index a21bd3af..4af8b7cb 100644 --- a/XmlSchemaClassGenerator/TypeModel.cs +++ b/XmlSchemaClassGenerator/TypeModel.cs @@ -1479,6 +1479,12 @@ public override CodeExpression GetDefaultValueFor(string defaultString, bool att "Parse", new CodePrimitiveExpression(defaultString)); return rv; } + else if (type == typeof(TimeSpan)) + { + var rv = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(CodeUtilities.CreateTypeReference(typeof(XmlConvert), Configuration)), + "ToTimeSpan", new CodePrimitiveExpression(defaultString)); + return rv; + } else if (type == typeof(bool) && !string.IsNullOrWhiteSpace(defaultString)) { if (defaultString == "0")