Skip to content

Commit 8204bee

Browse files
author
Michael Ganss
committed
Merge branch 'master' of github.com:mganss/XmlSchemaClassGenerator
2 parents 7b2b6c7 + 4df6db1 commit 8204bee

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

XmlSchemaClassGenerator/CodeUtilities.cs

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System;
1+
using Microsoft.CSharp;
2+
3+
using System;
24
using System.CodeDom;
35
using System.Collections.Generic;
46
using System.Collections.Immutable;
@@ -172,8 +174,7 @@ public static string GetUniqueFieldName(this TypeModel typeModel, PropertyModel
172174
var classModel = typeModel as ClassModel;
173175
var propBackingFieldName = propertyModel.Name.ToBackingField(classModel?.Configuration.PrivateMemberPrefix);
174176

175-
if (!IsValidIdentifier(propBackingFieldName.ToLower()))
176-
propBackingFieldName = "@" + propBackingFieldName;
177+
propBackingFieldName = CSharp.CreateEscapedIdentifier(propBackingFieldName);
177178

178179
if (classModel == null)
179180
return propBackingFieldName;
@@ -214,7 +215,7 @@ public static string GetUniquePropertyName(this TypeModel tm, string name)
214215

215216
internal static string NormalizeNewlines(string text) => NormalizeNewlinesRegex.Replace(text, "$1\r\n");
216217

217-
private static readonly Predicate<string> IsValidIdentifier = new Microsoft.CSharp.CSharpCodeProvider().IsValidIdentifier;
218+
private static readonly CSharpCodeProvider CSharp = new();
218219

219220
internal static Uri CreateUri(string uri) => string.IsNullOrEmpty(uri) ? null : new Uri(uri);
220221

@@ -255,7 +256,9 @@ public static bool IsUsingNamespace(string namespaceName, GeneratorConfiguration
255256

256257
public static CodeTypeReference CreateTypeReference(Type type, GeneratorConfiguration conf)
257258
{
258-
if (IsUsingNamespace(type.Namespace, conf))
259+
// If the type is a keyword it will prefix it with an @ to make it a valid identifier.
260+
var isKeyword = CSharp.CreateEscapedIdentifier(CSharp.GetTypeOutput(new(type)))[0] == '@';
261+
if (!isKeyword && IsUsingNamespace(type.Namespace, conf))
259262
{
260263
var typeRef = new CodeTypeReference(type.Name, conf.CodeTypeReferenceOptions);
261264

0 commit comments

Comments
 (0)