有没有一种编程方法来识别 C# 保留字?
我正在寻找一个功能,就像
public bool IsAReservedWord(string TestWord)
我知道我可以通过从 MSDN 获取保留字列表来推出自己的功能。不过,我希望语言或 .NET 反射中内置了一些可以依赖的东西,这样当我迁移到较新版本的 C#/.NET 时就不必重新访问该函数。
我寻找这个的原因是我正在寻找 .tt 文件代码生成的保护措施。
I'm looking for a function like
public bool IsAReservedWord(string TestWord)
I know I could roll my own by grabbing a reserve word list from MSDN. However I was hoping there was something built into either the language or .NET reflection that could be relied upon so I wouldn't have to revisit the function when I move to newer versions of C#/.NET.
The reason I'm looking for this is I'm looking for a safeguard in .tt file code generation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Microsoft.CSharp.CSharpCodeGenerator
有一个IsKeyword(string)
方法来完成此任务。但是,该类是内部类,因此您必须使用反射来访问它,并且不能保证它在 .NET 框架的未来版本中可用。请注意,IsKeyword
不考虑不同版本的 C#。公共方法
System.CodeDom.Compiler.ICodeGenerator.IsValidIdentifier(string)
也拒绝关键字。缺点是此方法还执行一些其他验证,因此其他非关键字字符串也会被拒绝。更新:如果您只需要生成有效的标识符而不是确定特定字符串是否为关键字,则可以使用
ICodeGenerator.CreateValidIdentifier(string)
。 此方法还可以通过在字符串前面添加一个下划线来处理带有两个前导下划线的字符串。对于关键字也是如此。请注意,ICodeGenerator.CreateEscapedIdentifier(string)
在此类字符串前添加了@
符号。以两个前导下划线开头的标识符是为实现(即 C# 编译器和关联的代码生成器等)保留的,因此在代码中避免使用此类标识符通常是一个好主意。
更新 2: 更喜欢
ICodeGenerator.CreateValidIdentifier
而不是ICodeGenerator.CreateEscapedIdentifier
的原因是__x
和@ __x
本质上是相同的标识符。以下内容将无法编译:如果编译器生成并使用
__x
标识符,并且用户将使用@__x
作为调用的结果CreateEscapedIdentifier
,会出现编译错误。使用CreateValidIdentifier
可以防止这种情况,因为自定义标识符会变成___x
(三个下划线)。The
Microsoft.CSharp.CSharpCodeGenerator
has anIsKeyword(string)
method that does exactly that. However, the class is internal, so you have to use reflection to access it and there's no guarantee it will be available in future versions of the .NET framework. Please note thatIsKeyword
doesn't take care of different versions of C#.The public method
System.CodeDom.Compiler.ICodeGenerator.IsValidIdentifier(string)
rejects keywords as well. The drawback is this method does some other validations as well, so other non-keyword strings are also rejected.Update: If you just need to produce a valid identifier rather than decide if a particular string is a keyword, you can use
ICodeGenerator.CreateValidIdentifier(string)
. This method takes care of strings with two leading underscores as well by prefixing them with one more underscore. The same holds for keywords. Note thatICodeGenerator.CreateEscapedIdentifier(string)
prefixes such strings with the@
sign.Identifiers startings with two leading underscores are reserved for the implementation (i.e. the C# compiler and associated code generators etc.), so avoiding such identifiers from your code is generally a good idea.
Update 2: The reason to prefer
ICodeGenerator.CreateValidIdentifier
overICodeGenerator.CreateEscapedIdentifier
is that__x
and@__x
are essentially the same identifier. The following won't compile:In case the compiler would generate and use a
__x
identifier, and the user would use@__x
as a result to a call toCreateEscapedIdentifier
, a compilation error would occur. When usingCreateValidIdentifier
this situation is prevented, because the custom identifier is turned into___x
(three underscores).请注意,自 v1.0 以来,C# 从未添加过新的 reserved 关键字。每个新关键字都是未保留的上下文关键字。
尽管我们将来当然有可能添加新的保留关键字,但我们已尽力避免这样做。
有关 C# 5 之前的所有保留关键字和上下文关键字的列表,请参阅
http://ericlippert.com/2009/05/11/reserved-and-contextual-keywords/
Note that C# has never added a new reserved keyword since v1.0. Every new keyword has been an unreserved contextual keyword.
Though it is of course possible that we might add a new reserved keyword in the future, we have tried hard to avoid doing so.
For a list of all the reserved and contextual keywords up to C# 5, see
http://ericlippert.com/2009/05/11/reserved-and-contextual-keywords/
由于
CreateEscapedIdentifier
的定义是:它将正确地将
__
标识符识别为保留。Since the definition of
CreateEscapedIdentifier
is:it will properly identify
__
identifiers as reserved.