|
1 | | -using System; |
| 1 | +using Microsoft.CSharp; |
| 2 | + |
| 3 | +using System; |
2 | 4 | using System.CodeDom; |
3 | 5 | using System.Collections.Generic; |
4 | 6 | using System.Collections.Immutable; |
@@ -172,8 +174,7 @@ public static string GetUniqueFieldName(this TypeModel typeModel, PropertyModel |
172 | 174 | var classModel = typeModel as ClassModel; |
173 | 175 | var propBackingFieldName = propertyModel.Name.ToBackingField(classModel?.Configuration.PrivateMemberPrefix); |
174 | 176 |
|
175 | | - if (!IsValidIdentifier(propBackingFieldName.ToLower())) |
176 | | - propBackingFieldName = "@" + propBackingFieldName; |
| 177 | + propBackingFieldName = CSharp.CreateEscapedIdentifier(propBackingFieldName); |
177 | 178 |
|
178 | 179 | if (classModel == null) |
179 | 180 | return propBackingFieldName; |
@@ -214,7 +215,7 @@ public static string GetUniquePropertyName(this TypeModel tm, string name) |
214 | 215 |
|
215 | 216 | internal static string NormalizeNewlines(string text) => NormalizeNewlinesRegex.Replace(text, "$1\r\n"); |
216 | 217 |
|
217 | | - private static readonly Predicate<string> IsValidIdentifier = new Microsoft.CSharp.CSharpCodeProvider().IsValidIdentifier; |
| 218 | + private static readonly CSharpCodeProvider CSharp = new(); |
218 | 219 |
|
219 | 220 | internal static Uri CreateUri(string uri) => string.IsNullOrEmpty(uri) ? null : new Uri(uri); |
220 | 221 |
|
@@ -255,7 +256,9 @@ public static bool IsUsingNamespace(string namespaceName, GeneratorConfiguration |
255 | 256 |
|
256 | 257 | public static CodeTypeReference CreateTypeReference(Type type, GeneratorConfiguration conf) |
257 | 258 | { |
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)) |
259 | 262 | { |
260 | 263 | var typeRef = new CodeTypeReference(type.Name, conf.CodeTypeReferenceOptions); |
261 | 264 |
|
|
0 commit comments