我用 roslyn 编译了一些诊断错误,显示我的代码如下:
using System;
using System.Reflection;
using System.Runtime.Loader;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;
using System.IO;
using Microsoft.CodeAnalysis;
namespace RoslynTest
{
public class Test
{
public static void Main(string[] args)
{
const string code =@"using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(""Hello, World!"");
}
}
}";
var tree = SyntaxFactory.ParseSyntaxTree(code);
var compilation = CSharpCompilation.Create("HelloWorldCompiled.exe", options: new CSharpCompilationOptions(OutputKind.ConsoleApplication),
syntaxTrees: new[] { tree }, references: new[] { MetadataReference.CreateFromFile(typeof(object).Assembly.Location) });
using (var stream = new MemoryStream())
{
var compileResult = compilation.Emit(stream);
var assembly = Assembly.Load(stream.GetBuffer());
assembly.EntryPoint.Invoke(null, BindingFlags.NonPublic | BindingFlags.Static, null, new object[] { null }, null);
}
}
}
}
Err 来到这一行:--> var compileResult = compilation.Emit(stream); 错误是:当前上下文中不存在名称“控制台”。(诊断错误)如何解决此错误