C# 中的关键字实现
有没有办法,以及如何实现我自己的关键字,例如 in
和 as
(等),以在我的代码中使用?
这就是我的想法。我想(我想只是出于我个人的原因)添加一些我自己的关键字,其中之一是“was
”关键字:
if(Control was Clicked)
{
// etc etc
}
Is there a way, and how would I go about implementing my own keyword such as in
, and as
(etc), to be used in my code?
Here is what I had in mind. I want to (just for my own personal reasons, I guess) add a few keywords of my own, one of which would be the "was
" keyword:
if(Control was Clicked)
{
// etc etc
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不。
你能得到的最接近的是扩展方法:
No.
The closest you could get would be an extension method:
不,您无法添加到 C# 语言,除非编写您自己的编译器。
但是,您的“was”关键字让我认为您可能正在寻找一种以声明方式处理事件的方法。 Microsoft 有一个名为“Reactive Extensions for .NET”的库(Rx )这是 LINQ 的扩展,允许您以声明方式处理事件。
No, you can't add to the C# language, short of writing your own compiler.
However, your "was" keyword makes me think you might be looking for a way to declaratively handle events. Microsoft have a library called "Reactive Extensions for .NET" (Rx) that is an extension to LINQ that allows you to deal with events in a declarative fashion.
不,您不能向 C# 添加关键字 - 至少在不自己编写编译器的情况下不能。但是,如果您想要做的事情很简单,也许您可以使用自定义预处理器来完成。不过,您可能会失去 Visual Studio 中的一些语法突出显示和错误检查功能。
CLR 的一种语言被设计为可扩展,是 Boo。
No, you cannot add keywords to C# - at least not without writing a compiler for yourself. If what you want to do is simple, however, perhaps you could do it using a custom preprocessor. You would lose some syntax highlighting and error checking in Visual Studio, though.
One language for the CLR, that is designed to be extensible like that, is Boo.