Programmers who are used to developing applications in
CodeDOM (.NET Framework 1.x) will encounter a warning in .NET Framework 2.0: “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.” Problems may
be with the code given below.
The Code snippet below explains the issue of .NET 1.x code
having problems in .NET 2.0. It explains the enriched functionality of the
CodeDomProvider class. The code uses namespaces like “System.CodeDom,”
“System.CodeDom.Compiler” and “Microsoft.CSharp.” The code below assumes that
the reader understands the syntax of C# language and is well versed with the
basic programming techniques.
Listing 4
ICodeCompiler iCodeCompiler = new CSharpCodeProvider().CreateCompiler();
iCodeCompiler.CompileAssemblyFromSource(compilerParameters, strcode);
In .NET Framework 2.0, CodeDomProvider itself encapsulates
all of the iCodeCompiler methods. Therefore, we better use a reference of the
CodeDOM provider and use it as shown below.
CodeDomProvider codeDomProvider = new CSharpCodeProvider();
CompilerResults compilerResults =
codeDomProvider.CompileAssemblyFromSource(compilerParameters, code);
The ICodeCompiler is an obsolete method and a programmer
should try to avoid it.