Keyword Implementation in C#
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
}
No.
The closest you could get would be an extension method:
Control.WasClicked()
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.
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.
精彩评论