dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.05k stars 4.03k forks source link

Make a built-in symbol for the nameof() operator #373

Open gafter opened 9 years ago

gafter commented 9 years ago

The compiler already provides symbols for built-in operators such as +(int,int). I think this would be one of those built-in operators, probably something like

public string nameof(object arg);

even though the argument might be something like a namespace.

This would (only) be visible through the SemanticModel API.

[Ported from TFS DevDiv 1078941]

Youssef1313 commented 2 years ago

Where the +(int, int) is exposed?

I tried:

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

CSharpCompilation comp = CSharpCompilation.Create(null, new[] { CSharpSyntaxTree.ParseText("class C { public void M() => _ = 5 + 5; }") }, new[] { MetadataReference.CreateFromFile(typeof(int).Assembly.Location) });
var model = comp.GetSemanticModel(comp.SyntaxTrees[0]);
var operation = model.GetOperation(comp.SyntaxTrees[0].GetRoot().DescendantNodes().OfType<BinaryExpressionSyntax>().Single());

But the resulting operation has a null OperatorMethod.