Reputation: 738
I'm trying to find a way to list all C# keywords. I need to make a comparison, such as:
if (key == "if" || key == "while" || <further_comparisons>)
{
// do something
}
It would be way better to do it by searching in a list of those keywords, and I would like to do it without typing them.
I'm looking at System.CodeDom namespace to see if I can find something.
If any of you could tell me where I could find it, I would really appreciate it. Thank you in advance!
Upvotes: 4
Views: 2290
Reputation: 221
Thanks to Roslyn, you can do this with the Microsoft.CodeAnalysis.CSharp nuget package.
After adding this package, your code becomes:
using Microsoft.CodeAnalysis.CSharp;
if (SyntaxFacts.GetKeywordKind(key) != SyntaxKind.None || SyntaxFacts.GetContextualKeywordKind(key) != SyntaxKind.None)
{
// do something
}
If you need this to be as fast as possible, use a static field like:
private static readonly HashSet<string> Keywords =
SyntaxFacts.GetKeywordKinds().Select(SyntaxFacts.GetText).ToHashSet();
Then, your code becomes:
if (Keywords.Contains(key))
{
// do something
}
Upvotes: 1
Reputation: 11801
If you don't mind using reflection and a dependency on implementation details, you can use the static IsKeyWord
method of the Microsoft.CSharp.CSharpCodeGenerator Class.
using System;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Reflection;
internal class CS
{
private static MethodInfo methIsKeyword;
static CS()
{
using (CSharpCodeProvider cs = new CSharpCodeProvider())
{
FieldInfo infoGenerator = cs.GetType().GetField("generator", BindingFlags.Instance | BindingFlags.NonPublic);
object gen = infoGenerator.GetValue(cs);
methIsKeyword = gen.GetType().GetMethod("IsKeyword", BindingFlags.Static | BindingFlags.NonPublic);
}
}
public static bool IsKeyword(string input)
{
return Convert.ToBoolean(methIsKeyword.Invoke(null, new object[] { input.Trim() }));
}
}
Example usage:
bool isKeyword = CS.IsKeyword("if");
Upvotes: 0
Reputation: 22406
CSharpCodeProvider
has the logic to do this. But you must call it with reflection. It contains an IsKeyword
function. More specifically, it has the actual list of keywords which IsKeyword
uses.
private static readonly string[][] keywords
Upvotes: 1
Reputation: 21337
You'll find a list of all keywords in the documentation: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/index
new string[]
{
"bool", "byte", "sbyte", "short", "ushort", "int", "uint", "long", "ulong", "double", "float", "decimal",
"string", "char", "void", "object", "typeof", "sizeof", "null", "true", "false", "if", "else", "while", "for", "foreach", "do", "switch",
"case", "default", "lock", "try", "throw", "catch", "finally", "goto", "break", "continue", "return", "public", "private", "internal",
"protected", "static", "readonly", "sealed", "const", "fixed", "stackalloc", "volatile", "new", "override", "abstract", "virtual",
"event", "extern", "ref", "out", "in", "is", "as", "params", "__arglist", "__makeref", "__reftype", "__refvalue", "this", "base",
"namespace", "using", "class", "struct", "interface", "enum", "delegate", "checked", "unchecked", "unsafe", "operator", "implicit", "explicit"
};
Upvotes: 1
Reputation: 222552
You can use
using Microsoft.CSharp;
CSharpCodeProvider cs = new CSharpCodeProvider();
then, you can use
var test = cs.IsValidIdentifier("if") //return false
Upvotes: 14