How do I make MonoDevelop recognize nameof syntax from C# 6.0?

那年仲夏 提交于 2019-12-25 02:24:28

问题


I'm in MonoDevelop v5.9.6. Although it seems to support C# 6.0, the editor doesn't recognize the nameof keyword, and it marks it red, because it tries to recognize it as if it were an identifier.

Is there any hack I can use to make it work in the editor, without breaking the compilation somehow?


回答1:


This hack works:

// hack to make MonoDevelop recognize nameof syntax from C#6.0
using nameof = System.Func<string>;

The editor recognizes it as "returning a string" and doesn't give any errors when used with an argument, like, for instance nameof(object.Equals).

When I Ctrl+Shift+Space to show the signature of the call, VSCode ignores it, and MonoDevelop shows this:

VSCode (which supports the nameof syntax) doesn't complain about it either. The lexer recognizes the nameof as a keyword (in red) but identifies it as a delegate when I hover.
In any case I don't get any errors.

What is even more intriguing, the compilers (both Mono and VS) simply ignore the using instruction and also accept usages of nameof, which works normally.

Now, the followup question would be... why though? Why won't the compilers complain about it?



来源:https://stackoverflow.com/questions/51457497/how-do-i-make-monodevelop-recognize-nameof-syntax-from-c-sharp-6-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!