c# - 当没有错误时,CSharpCodeProvider 不会返回编译器警告

标签 c# .net compilation compiler-construction .net-4.0

我正在使用 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/

相关文章:

Java语法变量声明产生式规则

c# - 将数组传递给非托管代码并在不复制的情况下返回

c# - C# 中的数字数据类型比较

c# - 接口(interface)作为一个用于继承的原因,而不是实现者

c# - 为什么在 .NET 中将返回类型 void 声明为 struct?

java - 如何在 Java 8 上编译 Eclipse 库

c# - .NET,每分钟(每分钟)发生一次事件。计时器是最好的选择吗?

C++ lib dll 的 C# 包装器类

c# - 系统.InvalidOperationException : Can not set Visibility

c# - Debug模式与 Release模式 - IsJITOptimizerDisabled 属性