Skip to content

Commit faf2d6f

Browse files
author
Michael Ganss
committed
Introduce .NET Core specific code configuration option (fixes #259)
1 parent 8365b4e commit faf2d6f

6 files changed

Lines changed: 26 additions & 4 deletions

File tree

XmlSchemaClassGenerator.Console/Program.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ static void Main(string[] args)
5353
var supportedCommentLanguages = new[] { "en", "de" };
5454
var uniqueTypeNamesAcrossNamespaces = false;
5555
var createGeneratedCodeAttributeVersion = true;
56+
var netCoreSpecificCode = false;
5657

5758
var options = new OptionSet {
5859
{ "h|help", "show this message and exit", v => showHelp = v != null },
@@ -124,6 +125,7 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
124125
v => commentLanguages = v.Split(',').Select(l => l.Trim()).ToArray() },
125126
{ "un|uniqueTypeNames", "generate type names that are unique across namespaces (default is false)", v => uniqueTypeNamesAcrossNamespaces = v != null },
126127
{ "gc|generatedCodeAttribute", "add version information to GeneratedCodeAttribute (default is true)", v => createGeneratedCodeAttributeVersion = v != null },
128+
{ "nc|netCore", "generate .NET Core specific code that might not work with .NET Framework (default is false)", v => netCoreSpecificCode = v != null },
127129
};
128130

129131
var globsAndUris = options.Parse(args);
@@ -197,7 +199,8 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
197199
SeparateSubstitutes = separateSubstitutes,
198200
CompactTypeNames = compactTypeNames,
199201
UniqueTypeNamesAcrossNamespaces = uniqueTypeNamesAcrossNamespaces,
200-
CreateGeneratedCodeAttributeVersion = createGeneratedCodeAttributeVersion
202+
CreateGeneratedCodeAttributeVersion = createGeneratedCodeAttributeVersion,
203+
NetCoreSpecificCode = netCoreSpecificCode
201204
};
202205

203206
generator.CommentLanguages.AddRange(commentLanguages);

XmlSchemaClassGenerator.Tests/Compiler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ public static Assembly GenerateFiles(string name, IEnumerable<string> files, Gen
111111
SeparateSubstitutes = generatorPrototype.SeparateSubstitutes,
112112
CompactTypeNames = generatorPrototype.CompactTypeNames,
113113
UniqueTypeNamesAcrossNamespaces = generatorPrototype.UniqueTypeNamesAcrossNamespaces,
114-
CreateGeneratedCodeAttributeVersion = generatorPrototype.CreateGeneratedCodeAttributeVersion
114+
CreateGeneratedCodeAttributeVersion = generatorPrototype.CreateGeneratedCodeAttributeVersion,
115+
NetCoreSpecificCode = generatorPrototype.NetCoreSpecificCode
115116
};
116117

117118
gen.CommentLanguages.Clear();

XmlSchemaClassGenerator.Tests/XmlTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ private static IEnumerable<string> ConvertXml(string name, IEnumerable<string> x
5555
MemberVisitor = generatorPrototype.MemberVisitor,
5656
CodeTypeReferenceOptions = generatorPrototype.CodeTypeReferenceOptions,
5757
DoNotForceIsNullable = generatorPrototype.DoNotForceIsNullable,
58-
CreateGeneratedCodeAttributeVersion = generatorPrototype.CreateGeneratedCodeAttributeVersion
58+
CreateGeneratedCodeAttributeVersion = generatorPrototype.CreateGeneratedCodeAttributeVersion,
59+
NetCoreSpecificCode = generatorPrototype.NetCoreSpecificCode
5960
};
6061

6162
gen.CommentLanguages.Clear();
@@ -310,7 +311,8 @@ public void TestSimple()
310311
GenerateInterfaces = true,
311312
NamespacePrefix = "Simple",
312313
GenerateDescriptionAttribute = true,
313-
CodeTypeReferenceOptions = CodeTypeReferenceOptions.GlobalReference
314+
CodeTypeReferenceOptions = CodeTypeReferenceOptions.GlobalReference,
315+
NetCoreSpecificCode = true
314316
});
315317
TestSamples("Simple", SimplePattern);
316318
}

XmlSchemaClassGenerator/CodeUtilities.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ public static Type GetEffectiveType(this XmlSchemaDatatype type, GeneratorConfig
178178
{
179179
XmlTypeCode.AnyAtomicType => typeof(string),// union
180180
XmlTypeCode.AnyUri or XmlTypeCode.GDay or XmlTypeCode.GMonth or XmlTypeCode.GMonthDay or XmlTypeCode.GYear or XmlTypeCode.GYearMonth => typeof(string),
181+
XmlTypeCode.Duration => configuration.NetCoreSpecificCode ? type.ValueType : typeof(string),
181182
XmlTypeCode.Time => typeof(DateTime),
182183
XmlTypeCode.Idref => typeof(string),
183184
XmlTypeCode.Integer or XmlTypeCode.NegativeInteger or XmlTypeCode.NonNegativeInteger or XmlTypeCode.NonPositiveInteger or XmlTypeCode.PositiveInteger => GetIntegerDerivedType(type, configuration, restrictions),

XmlSchemaClassGenerator/Generator.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,12 @@ public bool CreateGeneratedCodeAttributeVersion
282282
set { _configuration.CreateGeneratedCodeAttributeVersion = value; }
283283
}
284284

285+
public bool NetCoreSpecificCode
286+
{
287+
get { return _configuration.NetCoreSpecificCode; }
288+
set { _configuration.NetCoreSpecificCode = value; }
289+
}
290+
285291
static Generator()
286292
{
287293
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

XmlSchemaClassGenerator/GeneratorConfiguration.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,5 +295,14 @@ public void WriteLog(string message)
295295
/// Adds version information to <see cref="System.CodeDom.Compiler.GeneratedCodeAttribute"/>. Default is true.
296296
/// </summary>
297297
public bool CreateGeneratedCodeAttributeVersion { get; set; } = true;
298+
299+
/// <summary>
300+
/// Generate code that works with .NET Core but might be incompatible with .NET Framework. Default is false.
301+
/// Specific differences:
302+
/// <list type="bullet">
303+
/// <item>Use <see cref="TimeSpan"/> for duration instead of string <see cref="string"/></item>
304+
/// </list>
305+
/// </summary>
306+
public bool NetCoreSpecificCode { get; set; }
298307
}
299308
}

0 commit comments

Comments
 (0)