Roslyn - 编译简单 class:"The type or namespace name 'string' could not be found..."

Roslyn - compiling simple class: "The type or namespace name 'string' could not be found..."

我正在使用 Roslyn API 生成和编译 class。生成的 class 如下所示:

namespace MyCompany.Product
{
    public class TestClass
    {
        public void Configure(string id)
        {
        }
    }
}

然而,当我编译它时,Emit(ted) 结果给出:

error CS0246: The type or namespace name 'string' could not be found (are you missing a using directive or an assembly reference?)

这里是执行编译的方法:

    private static readonly IEnumerable<string> DefaultNamespaces = new[]
        {
            "System",
            "System.IO",
            "System.Net",
            "System.Linq",
            "System.Text",
            "System.Text.RegularExpressions",
            "System.Collections.Generic"
        };

    public void Compile(IEnumerable<SyntaxTree> syntaxes, string targetPath)
    {

        var assemblyPath = Path.GetDirectoryName(typeof(object).Assembly.Location);
        // assemblyPath = "C:\Windows\Microsoft.NET\Framework64\v4.0.30319"

        IEnumerable<MetadataReference> defaultReferences = new[]
        {
            MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "mscorlib.dll")),
            MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.dll")),
            MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.Core.dll")),
            MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.Runtime.dll")),

        };
        CSharpCompilationOptions defaultCompilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
                    .WithOverflowChecks(true).WithOptimizationLevel(OptimizationLevel.Release)
                    .WithUsings(DefaultNamespaces);

        CSharpCompilation compilation = CSharpCompilation.Create(
            targetPath,
            syntaxTrees: syntaxes,
            references: defaultReferences,
            options: defaultCompilationOptions);

        using (var ms = new MemoryStream())
        {
            EmitResult result = compilation.Emit(ms);
            // here, result.Success = false, with it's Diagnostics collection containing the error
            if (!result.Success)
            {
            } 
        }
    }

但是,如果我编译下面的 class,但使用 'string' 类型的常量,它会按预期编译,所以它不喜欢 'string' 声明为方法参数类型:

namespace MyCompany.Product
{
    public class TestClass
    {
        public const string Id = "e1a64bdc-936d-47d9-aa10-e8634cdd7070";
    }
}

我正在使用框架 4.6.1,Microsoft.CodeAnalysis 版本=1.3.1.0

我将您的第一个示例片段输入 CSharpSyntaxTree.ParseText,并将结果输入 Compile - 它按预期成功编译。

我猜你编译的节点类型有问题。 string 是一个 C# 关键字,它是 System.String 类型的别名,本身不是类型名称。

您可以使用 Roslyn 语法可视化工具来检查您自己的 SyntaxTreeCSharpSyntaxTree.ParseText 根据预期输出生成的语法之间的差异。