【发布时间】:2011-03-03 05:20:02
【问题描述】:
我正在使用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。
【问题讨论】:
-
顺便说一句,请参阅stackoverflow.com/questions/2610886/… 关于
GenerateInMemory -
@abatishchev 谢谢,这是一个有趣的事实。
标签: c# .net compilation compiler-construction .net-4.0