I'm trying to compile the C# code from this post:
using System;
// < ... omitted ... >
using Roslyn.Compilers;
using Roslyn.Compilers.CSharp;
using Roslyn.Services;
using Roslyn.Services.CSharp;
pip
or npm
the package name is easily found from the FQNnuget
I can't find the corresponding package (neither with google search nor with Nuget search)$ nuget install Roslyn.Compilers.CSharp # <--- use raw fqn, doesn't work
# ... omitted ...
Unable to find package 'Roslyn.Compilers.CSharp'
$ nuget install Microsoft.Net.Compilers.Toolset # <--- 1st result Nuget search
$ nuget install Microsoft.CodeAnalysis.Compilers # <--- 2nd result Nuget search
$ nuget install Microsoft.CodeAnalysis # <--- 3rd result Nuget search
I (semi) blindly tried some other packages but none worked:
main.cs(5,7): error CS0246: The type or namespace name `Roslyn' could not be found. Are you missing an assembly reference?
How can I systematically infer the Nuget relevant package from the fqn?
The nuget package you are looking for is Microsoft.CodeAnalysis.CSharp
. Add it to your project with:
dotnet add package Microsoft.CodeAnalysis.CSharp
Also, some things have changed, the code from the post you linked works with .NET 6 if changed like this:
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
namespace gettingstarted2
{
class Program
{
static void Main(string[] args)
{
SyntaxTree tree = CSharpSyntaxTree.ParseText(
@"using System;
using System.Collections;
using System.Linq;
using System.Text;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(""Hello, World!"");
}
}
}");
var root = (CSharpSyntaxNode)tree.GetRoot();
}
}
}