Search code examples
c#codedom

How to force CSharpCodeProvider to compile with a specific target framework


I've got a small app, which can compile .cs Scripts using CSharpCodeProvider It worked fine for years. Now I've tried a new script and I run into compilation issues. I tracked it down to the usage of a Func delegate with 5 parameters. I know, that there was a switch with .NET Framework 4.0, allowing more than 4 parameters.

Yes, I tried to force the usage of a required compiler version:

var compiler = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v4.0" } });

But it doesn't help. Whenever I add Func parameter #5, the script does not compile, which leads me to the conclusion, it's somehow using the old compiler.

Any ideas, how to fix this?

I've searched Stackoverflow for similar problems but haven't found any useful answer. The usage of Roslyn is not an alternative, since it includes a lot of dependencies, which I would like to avoid in this small script app.


Solution

  • This is an example for a problem, which cannot be solved on Friday afternoon, but is easily fixed on Monday morning.

    I found out, that my new VisualStudio 2022, when updating the solution, somehow has not taken over the old OutputPath of the app. So I had a new script, but still an old app, which used the v3.5 compiler.

    Means, the CompilerVersion setting works as expected.

    Sorry for the inconvenience.