CodeDomProvider.CreateCompiler()已过时

时间:2013-01-18 19:19:10

标签: c# obsolete

CSharpCodeProvider codeProvider = new CSharpCodeProvider();
ICodeCompiler MyCompiler = codeProvider.CreateCompiler();
CompilerParameters myParameters = new CompilerParameters();
myParameters.GenerateExecutable = true;
myParameters.OutputAssembly = @"prog.exe";
myParameters.TreatWarningsAsErrors = false;
CompilerResults results = myCompiler.CompileAssemblyFromFile(myParameters,"prog.txt");

如何编写此代码以消除此警告:

  

System.CodeDom.Compiler.CodeDomProvider.CreateCompiler()'已过时:'“调用者不应使用ICodeCompiler接口,而应直接在CodeDomProvider类上使用这些方法。从CodeDomProvider继承的方法仍然必须实现此接口,并且应排除此警告或废弃此方法。**

2 个答案:

答案 0 :(得分:8)

改为使用this method

CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");

扩展:

if (CodeDomProvider.IsDefinedLanguage(language))
{
    CodeDomProvider provider = CodeDomProvider.CreateProvider(language);
    // ...
}
else
    Console.WriteLine("ERROR");

答案 1 :(得分:1)

CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CompilerParameters myParameters = new CompilerParameters();
myParameters.GenerateExecutable = true;
myParameters.OutputAssembly = @"prog.exe";
myParameters.TreatWarningsAsErrors = false;
CompilerResults results = codeProvider.CompileAssemblyFromFile(myParameters, "prog.txt");