我正在使用 CSharpCodeProvider
类来编译我在应用程序中用作 DSL 的 C# 脚本。当有警告但没有错误时,生成的 CompilerResults
实例的 Errors
属性不包含任何项目。但是当我引入错误时,警告也会突然列在 Errors
属性中。
string script = @"
using System;
using System; // generate a warning
namespace MyNamespace
{
public class MyClass
{
public void MyMethod()
{
// uncomment the next statement to generate an error
//intx = 0;
}
}
}
";
CSharpCodeProvider provider = new CSharpCodeProvider(
new Dictionary<string, string>()
{
{ "CompilerVersion", "v4.0" }
});
CompilerParameters compilerParameters = new CompilerParameters();
compilerParameters.GenerateExecutable = false;
compilerParameters.GenerateInMemory = true;
CompilerResults results = provider.CompileAssemblyFromSource(
compilerParameters,
script);
foreach (CompilerError error in results.Errors)
{
Console.Write(error.IsWarning ? "Warning: " : "Error: ");
Console.WriteLine(error.ErrorText);
}
那么如何在没有错误的情况下获取警告呢?
顺便说一下,我不想将 TreatWarningsAsErrors
设置为 true
。
最佳答案
您没有设置CompilerParameters.WarningLevel
关于c# - 当没有错误时,CSharpCodeProvider 不会返回编译器警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3032391/