diff --git a/XmlSchemaClassGenerator/CodeUtilities.cs b/XmlSchemaClassGenerator/CodeUtilities.cs index adbda9cc..39e322bf 100644 --- a/XmlSchemaClassGenerator/CodeUtilities.cs +++ b/XmlSchemaClassGenerator/CodeUtilities.cs @@ -1,4 +1,6 @@ -using System; +using Microsoft.CSharp; + +using System; using System.CodeDom; using System.Collections.Generic; using System.Collections.Immutable; @@ -172,7 +174,7 @@ public static string GetUniqueFieldName(this TypeModel typeModel, PropertyModel var classModel = typeModel as ClassModel; var propBackingFieldName = propertyModel.Name.ToBackingField(classModel?.Configuration.PrivateMemberPrefix); - if (!IsValidIdentifier(propBackingFieldName.ToLower())) + if (!CSharp.IsValidIdentifier(propBackingFieldName.ToLower())) propBackingFieldName = "@" + propBackingFieldName; if (classModel == null) @@ -214,7 +216,7 @@ public static string GetUniquePropertyName(this TypeModel tm, string name) internal static string NormalizeNewlines(string text) => NormalizeNewlinesRegex.Replace(text, "$1\r\n"); - private static readonly Predicate IsValidIdentifier = new Microsoft.CSharp.CSharpCodeProvider().IsValidIdentifier; + private static readonly CSharpCodeProvider CSharp = new(); internal static Uri CreateUri(string uri) => string.IsNullOrEmpty(uri) ? null : new Uri(uri); @@ -255,7 +257,9 @@ public static bool IsUsingNamespace(string namespaceName, GeneratorConfiguration public static CodeTypeReference CreateTypeReference(Type type, GeneratorConfiguration conf) { - if (IsUsingNamespace(type.Namespace, conf)) + // If the type is a keyword it will prefix it with an underscore to make it a valid identifier. + var isKeyword = CSharp.CreateValidIdentifier(CSharp.GetTypeOutput(new(type)))[0] == '_'; + if (!isKeyword && IsUsingNamespace(type.Namespace, conf)) { var typeRef = new CodeTypeReference(type.Name, conf.CodeTypeReferenceOptions);