C# 将命令行参数传递给roslyn编译器
我想知道在编译和执行代码时,是否有方法将命令行参数传递给roslyn 我当前用于生成编译器和执行源代码的代码如下所示:C# 将命令行参数传递给roslyn编译器,c#,.net-core,roslyn,roslyn-code-analysis,C#,.net Core,Roslyn,Roslyn Code Analysis,我想知道在编译和执行代码时,是否有方法将命令行参数传递给roslyn 我当前用于生成编译器和执行源代码的代码如下所示: CSharpCompilation compilation = CSharpCompilation.Create(Path.GetFileName(assemblyPath)) .WithOptions(new CSharpCompilationOptions(OutputKind.ConsoleApplication)) .AddReferences(referenc
CSharpCompilation compilation = CSharpCompilation.Create(Path.GetFileName(assemblyPath))
.WithOptions(new CSharpCompilationOptions(OutputKind.ConsoleApplication))
.AddReferences(references)
.AddSyntaxTrees(syntaxTree);
using (var memStream = new MemoryStream())
{
var result = compilation.Emit(memStream);
if (!result.Success)
{
IEnumerable<Diagnostic> failures = result.Diagnostics.Where(diagnostic =>
diagnostic.IsWarningAsError ||
diagnostic.Severity == DiagnosticSeverity.Error);
foreach (Diagnostic diagnostic in failures)
{
Console.Error.WriteLine("{0}: {1}", diagnostic.Id, diagnostic.GetMessage());
}
}
else
{
memStream.Seek(0, SeekOrigin.Begin);
Assembly assembly = Assembly.Load(memStream.ToArray());
var test = assembly;
}
}
using System;
class Program
{
static string StringToUppercase(string str)
{
return str.ToUppercase();
}
static void Main(string[] args)
{
string str = Console.ReadLine();
string result = StringToUppercase(str);
Console.WriteLine(result);
}
}
在这里,我可以传入一个命令行参数并获得Console.WriteLine()的结果。处理这个问题的最佳方法是什么?当前代码确实构建并将编译和分析源代码
代码将在.NET core 3.1 mvc web应用程序中编写。我猜您希望将参数传递给已编译的应用程序,而不是roslyn:)
执行编译后的代码并使用以获取其输出(示例:): 或者为其启动一个新流程:
memStream.CopyTo(File.OpenWrite("temp.exe"))
// Copied from MSDN
// https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.standardoutput
using (Process process = new Process())
{
process.StartInfo.FileName = "temp.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
// Synchronously read the standard output of the spawned process.
StreamReader reader = process.StandardOutput;
string output = reader.ReadToEnd();
// Write the redirected output to this application's window.
Console.WriteLine(output);
process.WaitForExit();
}
memStream.CopyTo(File.OpenWrite("temp.exe"))
// Copied from MSDN
// https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.standardoutput
using (Process process = new Process())
{
process.StartInfo.FileName = "temp.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
// Synchronously read the standard output of the spawned process.
StreamReader reader = process.StandardOutput;
string output = reader.ReadToEnd();
// Write the redirected output to this application's window.
Console.WriteLine(output);
process.WaitForExit();
}