Getting CSharpCodeProvider to use .NET 3.5

  • Thread starter Thread starter Ravi Bhavnani
  • Start date Start date
R

Ravi Bhavnani

I have a .NET 3.5 app (built using VS2008 RTM, no CTPs installed) that uses
CSharpCodeProvider to compile some generated code that requires .NET 3.5.
When I set the compiler's .NET version to 3.5 (per the MSDN example), I get
a "csc.exe not found" exception thrown when I try to compile. Here's what
I'm doing:


// Set up compiler parameters CompilerParameters cp = new
CompilerParameters(); cp.xxx = xxx; // Specify .NET version
Dictionary<string,string> providerOptions = new Dictionary<string,string>();
providerOptions.Add ("CompilerVersion", "3.5"); CSharpCodeProvider
codeProvider = new CSharpCodeProvider (providerOptions); // Do the compile
string sourceCode = "..."; CompilerResults cr =
codeProvider.CompileAssemblyFromSource (cp, sourceCode);If I use
CSharpCodeProvider's default constructor, the code compiles if it's .NET 2.0
compliant, but (expectedly) generates an "unknown reference" error if I try
to reference a .NET 3.5 stock assembly.

A nudge in the right direction would be appreciated.

Thanks!

/ravi
 
Ravi Bhavnani said:
I have a .NET 3.5 app (built using VS2008 RTM, no CTPs installed) that uses
CSharpCodeProvider to compile some generated code that requires .NET 3.5.
When I set the compiler's .NET version to 3.5 (per the MSDN example), I get
a "csc.exe not found" exception thrown when I try to compile. Here's what
I'm doing:


// Set up compiler parameters CompilerParameters cp = new
CompilerParameters(); cp.xxx = xxx; // Specify .NET version
Dictionary<string,string> providerOptions = new Dictionary<string,string>();
providerOptions.Add ("CompilerVersion", "3.5"); CSharpCodeProvider
codeProvider = new CSharpCodeProvider (providerOptions); // Do the compile
string sourceCode = "..."; CompilerResults cr =
codeProvider.CompileAssemblyFromSource (cp, sourceCode);If I use
CSharpCodeProvider's default constructor, the code compiles if it's .NET 2.0
compliant, but (expectedly) generates an "unknown reference" error if I try
to reference a .NET 3.5 stock assembly.

A nudge in the right direction would be appreciated.

Try setting the compiler version to "v3.5" instead of "3.5". I know it
goes against the docs, but it works in the app I've written to compile
snippets of code.
 
Back
Top