Pregunta

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");

How can I write this code to eliminate this warning:

System.CodeDom.Compiler.CodeDomProvider.CreateCompiler()' is obsolete: '"Callers should not use the ICodeCompiler interface and should instead use the methods directly on the CodeDomProvider class. Those inheriting from CodeDomProvider must still implement this interface, and should exclude this warning or also obsolete this method.**

¿Fue útil?

Solución

Use this method instead:

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

Extended:

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

Otros consejos

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");
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top