Necromancing here.
In .NET Core/DNX, you can do it with Roslyn-SyntaxFacts
Microsoft.CodeAnalysis.CSharp.SyntaxFacts.IsReservedKeyword( Microsoft.CodeAnalysis.CSharp.SyntaxFacts.GetKeywordKind("protected") ); foreach (ColumnDefinition cl in tableColumns) { sb.Append(@" public "); sb.Append(cl.DOTNET_TYPE); sb.Append(" "); // for keywords //if (!Microsoft.CodeAnalysis.CSharp.SyntaxFacts.IsValidIdentifier(cl.COLUMN_NAME)) if (Microsoft.CodeAnalysis.CSharp.SyntaxFacts.IsReservedKeyword( Microsoft.CodeAnalysis.CSharp.SyntaxFacts.GetKeywordKind(cl.COLUMN_NAME) )) sb.Append("@"); sb.Append(cl.COLUMN_NAME); sb.Append("; // "); sb.AppendLine(cl.SQL_TYPE); } // Next cl
Or in the old variant with Codedom - After a look in the mono sourcecode:
CodeDomProvider.cs
public virtual bool IsValidIdentifier (string value) 286 { 287 ICodeGenerator cg = CreateGenerator (); 288 if (cg == null) 289 throw GetNotImplemented (); 290 return cg.IsValidIdentifier (value); 291 } 292
Then CSharpCodeProvider.cs
public override ICodeGenerator CreateGenerator() 91 { 92 #if NET_2_0 93 if (providerOptions != null && providerOptions.Count > 0) 94 return new Mono.CSharp.CSharpCodeGenerator (providerOptions); 95 #endif 96 return new Mono.CSharp.CSharpCodeGenerator(); 97 }
Then CSharpCodeGenerator.cs
protected override bool IsValidIdentifier (string identifier) { if (identifier == null || identifier.Length == 0) return false; if (keywordsTable == null) FillKeywordTable (); if (keywordsTable.Contains (identifier)) return false; if (!is_identifier_start_character (identifier [0])) return false; for (int i = 1; i < identifier.Length; i ++) if (! is_identifier_part_character (identifier [i])) return false; return true; } private static System.Collections.Hashtable keywordsTable; private static string[] keywords = new string[] { "abstract","event","new","struct","as","explicit","null","switch","base","extern", "this","false","operator","throw","break","finally","out","true", "fixed","override","try","case","params","typeof","catch","for", "private","foreach","protected","checked","goto","public", "unchecked","class","if","readonly","unsafe","const","implicit","ref", "continue","in","return","using","virtual","default", "interface","sealed","volatile","delegate","internal","do","is", "sizeof","while","lock","stackalloc","else","static","enum", "namespace", "object","bool","byte","float","uint","char","ulong","ushort", "decimal","int","sbyte","short","double","long","string","void", "partial", "yield", "where" }; static void FillKeywordTable () { lock (keywords) { if (keywordsTable == null) { keywordsTable = new Hashtable (); foreach (string keyword in keywords) { keywordsTable.Add (keyword, keyword); } } } } static bool is_identifier_start_character (char c) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || c == '@' || Char.IsLetter (c); } static bool is_identifier_part_character (char c) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || (c >= '0' && c <= '9') || Char.IsLetter (c); }
You get this code:
public static bool IsValidIdentifier (string identifier) { if (identifier == null || identifier.Length == 0) return false; if (keywordsTable == null) FillKeywordTable(); if (keywordsTable.Contains(identifier)) return false; if (!is_identifier_start_character(identifier[0])) return false; for (int i = 1; i < identifier.Length; i++) if (!is_identifier_part_character(identifier[i])) return false; return true; } internal static bool is_identifier_start_character(char c) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || c == '@' || char.IsLetter(c); } internal static bool is_identifier_part_character(char c) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || (c >= '0' && c <= '9') || char.IsLetter(c); } private static System.Collections.Hashtable keywordsTable; private static string[] keywords = new string[] { "abstract","event","new","struct","as","explicit","null","switch","base","extern", "this","false","operator","throw","break","finally","out","true", "fixed","override","try","case","params","typeof","catch","for", "private","foreach","protected","checked","goto","public", "unchecked","class","if","readonly","unsafe","const","implicit","ref", "continue","in","return","using","virtual","default", "interface","sealed","volatile","delegate","internal","do","is", "sizeof","while","lock","stackalloc","else","static","enum", "namespace", "object","bool","byte","float","uint","char","ulong","ushort", "decimal","int","sbyte","short","double","long","string","void", "partial", "yield", "where" }; internal static void FillKeywordTable() { lock (keywords) { if (keywordsTable == null) { keywordsTable = new System.Collections.Hashtable(); foreach (string keyword in keywords) { keywordsTable.Add(keyword, keyword); } } } }