问题
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