I'm trying to do something with nameof
expressions in a CSharpSyntaxWalker
, however, I noticed that there is no NameOfExpressionSyntax
in the AST. Instead I get an InvocationExpressionSyntax
for which SemanticModel.GetSymbolInfo
returns no matching symbols, and the expression of the invocation is an IdentifierNameSyntax
containing an identifier token "nameof"
.
So to recognize nameof
expressions I would have added a special case to VisitInvocationExpression
, looking for whether GetSymbolInfo
returns anything and if not, looking for whether the identifier is nameof
. However, that sounds a bit iffy to me. Is there a better way maybe which shifts that sort of detection logic to the parser?
(P.S.: I know this is probably parsed like this for backwards compatibility reasons; just wondering whether there is an API for distinguishing nameof
and normal invocations.)
nameof()
method prior to C# 6, there is no way to guarantee that it isn't referring to a method in the class or imported via astatic using
. At the time of syntax parsing there is no notion of a semantic model yet and afterwards it is obviously too late. Anyone doingnameof
parsing has to wire up their own solution ;-) – Torso