CodeDomProvider.CreateCompiler - need a replacement.

I am posting it second time today. For some strange reason the first post disappered after an hour or so. It is about the method: CodeDomProvider.CreateCompiler(). The method appears to be obsolete but it is used in a code sample I have copied from MSDN. There should be a replacement for this method. I would like to know what it is.

Thanks.


Answer this question

CodeDomProvider.CreateCompiler - need a replacement.

  • Narcis Calvet

    Yeah, thanks again! I think a lot of the confusion is because the error message generated by trying to call CreateCompiler method attempted to say that in "MSSpeak" (which is sometimes analgous to "Greenspanspeak") and all the examples that I found to use CodeDomProvider had calls to CreateCompiler first since I don't think all the docs are in sync yet.

    So anyway, thanks for putting it more clearly. This part of .NET is very interesting to work with.

    dlesko


  • mc_hk

    "So in the examples where CreateCompiler is called, we just use a provider instead (Basically just commenting out that line and it works )

    Then as I understand it, a "provider" in 2.0 replaces a "compiler" in 1.1 "

    Yep in .NET 2.0 all the methods of the compiler (and code code generator and parser)were moved directly to the CodeDomProvider itself. Underneath the covers a compiler is still being created via a call to CreateCompiler, but you as a CodeDomProvider client are no longer required to explicitly create the compiler with a call to CreateCompiler.

    My guess is that this was done so that CodeDomProvider can be more easily enhanced with additional methods and features in future versions (i.e. move to an abstract base class model instead of an interface based model)

    Glad to hear you got it working!


  • ThierryAsked

    It is too abstract for me to understand at this point of what you've said. I tried to play with the code based on your pointers but always ran into one error or another.

    Support in this forum is rotten.

  • jeymard

    OK, that's it - I got it now. Thanks John! The CreateCompiler() call is simply replaced with a CodeProvider for the language you are using. For the first poster, here's a working example in VB -

    Public Function CompileScript(ByVal Source As String) As CompilerResults
    Dim provider As CodeDomProvider = New VBCodeProvider()
    Dim params As New CompilerParameters()
    Dim results As CompilerResults = Nothing

    With params
    .GenerateExecutable = False
    .GenerateInMemory = True
    .IncludeDebugInformation = False
    Dim refs() As String = {"System.dll", "Microsoft.VisualBasic.dll"}
    .ReferencedAssemblies.AddRange(refs)
    End With

    Try
    results = provider.CompileAssemblyFromSource(params, Source)
    Catch ex As Exception
    Debug.Print(ex.Message)
    End Try

    Return results
    End Function

    Thanks - dlesko


  • claude_c

    Hi John,

    If you know how, can you please explain how to modify this sample code so that it uses the 2.0 CodeDomProvider instance as you describe

    Public Shared Function CompileCode(ByVal provider As CodeDomProvider, _
    ByVal sourceFile As string, _
    ByVal exeFile As string) As CompilerResults
    ' Obtain an ICodeCompiler from the CodeDomProvider.
    Dim compiler As ICodeCompiler = provider.CreateCompiler()

    ' Configure a CompilerParameters that links System.dll
    ' and produces the specified executable file.
    Dim referenceAssemblies As String() = {"System.dll"}
    Dim cp As New CompilerParameters(referenceAssemblies, exeFile, False)

    ' Generate an executable rather than a DLL file.
    cp.GenerateExecutable = True

    ' Invoke compilation.
    Dim cr As CompilerResults = compiler.CompileAssemblyFromFile(cp, _
    sourceFile)
    ' Return the results of compilation.
    Return cr
    End Function
    Public Shared Function CompileCode(ByVal provider As CodeDomProvider, _
    ByVal sourceFile As string, _
    ByVal exeFile As string) As CompilerResults
    ' Obtain an ICodeCompiler from the CodeDomProvider.
    Dim compiler As ICodeCompiler = provider.CreateCompiler()

    ' Configure a CompilerParameters that links System.dll
    ' and produces the specified executable file.
    Dim referenceAssemblies As String() = {"System.dll"}
    Dim cp As New CompilerParameters(referenceAssemblies, exeFile, False)

    ' Generate an executable rather than a DLL file.
    cp.GenerateExecutable = True

    ' Invoke compilation.
    Dim cr As CompilerResults = compiler.CompileAssemblyFromFile(cp, _
    sourceFile)
    ' Return the results of compilation.
    Return cr
    End Function

    As far as I can tell, there are no working examples of using the CodeDomProvider to actually compile.

    Thanks - dlesko


  • Manuel L.

    So,

    CodeDomProvider provider = new CSharpCodeProvider();

    Replaces the "Provider".CreateCompiler call

    So in the examples where CreateCompiler is called, we just use a provider instead (Basically just commenting out that line and it works )

    Then as I understand it, a "provider" in 2.0 replaces a "compiler" in 1.1

    thanks for answering...

    dlesko


  • Paul Looijmans

    All the methods from the ICodeCompiler interface have been moved to CodeDomProvider itself.  So you dont need to call CreateCompiler on a CodeDomProvider instance.  You just use the CodeDomProvider instance itself.

    FYI: this is described in the MSDN docs for ICodeCompiler

    Hope that helps.


  • Leo Kent

    Heres a sample in C#. Its essentially identical to your VB code, so if you were having trouble, I'm not sure what the trouble is. What errors were you seeing I tested my sample code and it seems to work just fine...


    using System;
    using System.CodeDom;
    using System.CodeDom.Compiler;
    using Microsoft.CSharp;


    namespace ConsoleApplication1
    {
    public class Program
    {
    static void Main( string[] args )
    {
    CodeDomProvider provider = new CSharpCodeProvider();

    CompilerParameters options = new CompilerParameters();
    options.GenerateExecutable = true;
    options.OutputAssembly = "Sample.exe";
    options.ReferencedAssemblies.Add( "System.dll");

    CompilerResults results = provider.CompileAssemblyFromFile(
    options, "ProgramToCompile.cs" );
    }
    }
    }


    Here is the contents of ProgramToCompile.cs

    using System;
    using System.Collections.Specialized;

    namespace SampleSource
    {
    public class Program
    {
    static void Main( string[] args )
    {
    // Defined in System.dll
    HybridDictionary table = new HybridDictionary();
    Console.WriteLine( "Created HybridDictionary" );
    }
    }
    }



  • CodeDomProvider.CreateCompiler - need a replacement.