none
CodeDOM, <providerOption name="CompilerVersion" value="3.5" /> not working?! and how to retrieve providerOption at runtime?

    Question

  • I've two issues with <providerOption> of CodeDOM introduced in .Net 3.5.

     

    1Q. I added the following to the app.config (VS2008),

     

    <configuration>

    ...

    <system.codedom>

    <compilers>

    <!-- zero or more compiler elements -->

    <compiler

    language="c#;cs;csharp"

    extension=".cs"

    type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">

    <providerOption name="CompilerVersion" value="3.5" />

    </compiler>

    </compilers>

    </system.codedom>

    </configuration>

     

    At code level, if I added ReferencedAssemblies.Add("System.Xml.Linq.dll"); and compile the codeDom, I got error saying,

      error CS0006: Metadata file 'System.Data.Linq.dll' could not be found

     

    However, if I explicitly set it at my codeDOM project (and removing <providerOption .../> at app.config,

    e.g.

    ComDomProvider provider = new Microsoft.CSharp.CSharpCodeProvider(new Dictionary<string, string>() {{"CompilerVersion", "v3.5"}});

     

    The above error was gone and the codeDom codes was compiled successfully.

     

    What did I do wrong?

     

    2Q. Assuming <providerOption...> in app.config could be working to set CSharpCodeProvider to whatever version, how can I retrieve providerOption content at code level so I can know what .Net version was stored at app.config?

     

    Thanks.

    Friday, February 01, 2008 11:48 PM

Answers

  • After debugging through the .Net framework source code I found that it expects a value of  "v3.5" for the providerOption instead of "3.5". Applying this value solved the problem.

    Wednesday, February 06, 2008 3:27 PM

All replies

  • To pick up the .config settings for CodeDOM, you need to create your CodeDOM provider via:

    var csc = CodeDomProvider.CreateProvider("csharp");

    instead of CSharpCodeProvider's constructor.


    To read the configuration file's content, recommended viewing the app.config file as XML file and using .net XML reading class to read it. You can read the last post in this thread as a good start. This discussion is also a good reference about it.


    Thanks!

    Monday, February 04, 2008 7:42 AM
  • Hi Feng,

     

    After I changed to use,

    CodeDomProvider csc=CodeDomProvider.CreateProvider("csharp");

     

    I got run-time InvalidOperationException saying "Compiler executable file csc.exe cannot be found." at CompilerResult cr = provider.CompileAssemblyFromFile(cp, "filename.cs");

     

    where cp was CompilerParameters type.

     

    My app.Config is as follows:-

    <configuration>

    ...

    <system.codedom>

    <compilers>

    <!-- zero or more compiler elements -->

    <compiler

    language="c#;cs;csharp"

    extension=".cs"

    type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">

    <providerOption name="CompilerVersion" value="3.5" />

    </compiler>

    </compilers>

    </system.codedom>

    <appSettings>

    ...

    </appSettings>

    </configuration>

     

    What did I do wrong?

     

    Thanks.

    Monday, February 04, 2008 8:07 AM
  • To troubleshoot this issue, we really need the source code and the detailed repro steps to reproduce the problem, so that we can investigate the issue in house. It is not necessary that you send out the complete source of your project. We just need a simplest sample to reproduce the problem. You can remove any confidential information or business logic from it.
    Monday, February 04, 2008 8:57 AM
  •  

    Here you go... Hopefully, there is an issue below instead of the bug... Thanks.

     

    private void GetCodeDOM(FileInfo csharpFileInfo)

    {

    // changed CodeDomProvider to var --> same error...

    //

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

     

    if (provider != null)

    {

    CompilerParameters cp = new CompilerParameters();

    cp.GenerateInMemory = true;

    cp.TreatWarningsAsErrors = false;

    }

     

    CompilerResults cr = provider.CompileAssemblyFromFile(cp, csharpFileInfo.FullName);

    if (cr.Errors.Count > 0)

    {

    Debug.WriteLine("Build error.");

    }

    }

    Monday, February 04, 2008 9:40 AM
  • I've reproduced it, and looks like this is an issue with the CSharpCodeProvider.

    You may also submit it to the following site in the formal way:

    http://connect.microsoft.com/VisualStudio/


    Tuesday, February 05, 2008 2:16 PM
  • After debugging through the .Net framework source code I found that it expects a value of  "v3.5" for the providerOption instead of "3.5". Applying this value solved the problem.

    Wednesday, February 06, 2008 3:27 PM
  • After debugging through the .Net Framework source code I found that it expects a value "v3.5" for the <providerOption> instead of "3.5". Applying this value solved the problem

     

    Wednesday, February 06, 2008 3:37 PM
  • Look like a documentation bug to me, thanks Igor. I will try when I get to the codes.

     

    Thanks.

    Thursday, February 07, 2008 1:04 AM
  • Hi Wood-MSDN

    this is not issue, we need to pass assemblies to CompilerParameters constructor .

    like

    CompilerParameters compilerParameters = new CompilerParameters(new[]
                                                                            { "mscorlib.dll", "System.Core.dll" }, "TestCCU.dll", true);

    its working...
    please check and let me know...


    Thank You

    Atul Katare
    _________________________

    Build Relationship Dynamically
    • Proposed as answer by AtulKatare Wednesday, July 15, 2009 7:20 AM
    Wednesday, July 15, 2009 6:28 AM
  • How was it working fine after removing v3.5 to 3.5....I don't think so...I go  the same issue but what I came to know is that 3.5 framework is not installed on the machine I was running....is that correct assumption to make.

     

     

    Thanks

    Bharath VasiReddy

    Tuesday, April 20, 2010 9:16 PM
  • Do you have Microsoft .Net framework 3.5 installed? - this could be the cause of your troubles.
    Friday, June 11, 2010 5:27 PM