How to dynamically use C# 3.5 compiler
-
For several months I've used dynamic compiler in my app that mounted a dll on start-up. But today I came across a painful problem. The code that can be compiled trouble-free in vc#08 has syntax errors according to the 'dynamic' compiler (let me call it dynamic for distinction). The piece of code in mind looks like this:
void foo()
{
//...
ActorDescription actorDesc = new ActorDescription()
{
BodyDescription = new BodyDescription(70)
};
//...
}The dynamic compiler shows an error :
"error CS1002: ; expected".
After simple test I managed to find out that the dynamic compiler probably aims at an older framework and I'm trying to use newer syntax features. In shortcut, what I was using is this:System.CodeDom.Compiler.CodeDomProvider cdp = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("CSharp"); System.CodeDom.Compiler.CompilerResults cr = cdp.CompileAssemblyFromFile(parameters, fullPaths);
The functions above are contained by System.dll v2.0 library, so it's not surprising it won't work. Anyway, System.dll v3.5 doesn't exist. Where is the new set of methods to compile according to the newest c# specification? What's the workaround for this? I've been googling for a few hours with no answer. Thanks in advance! :)
-
For several months I've used dynamic compiler in my app that mounted a dll on start-up. But today I came across a painful problem. The code that can be compiled trouble-free in vc#08 has syntax errors according to the 'dynamic' compiler (let me call it dynamic for distinction). The piece of code in mind looks like this:
void foo()
{
//...
ActorDescription actorDesc = new ActorDescription()
{
BodyDescription = new BodyDescription(70)
};
//...
}The dynamic compiler shows an error :
"error CS1002: ; expected".
After simple test I managed to find out that the dynamic compiler probably aims at an older framework and I'm trying to use newer syntax features. In shortcut, what I was using is this:System.CodeDom.Compiler.CodeDomProvider cdp = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("CSharp"); System.CodeDom.Compiler.CompilerResults cr = cdp.CompileAssemblyFromFile(parameters, fullPaths);
The functions above are contained by System.dll v2.0 library, so it's not surprising it won't work. Anyway, System.dll v3.5 doesn't exist. Where is the new set of methods to compile according to the newest c# specification? What's the workaround for this? I've been googling for a few hours with no answer. Thanks in advance! :)
CodeDom should use whatever the newest installed compiler is. I assume that if you installed VS 2010 you'd have the C# v4.0 compiler and it should work fine. (I haven't, so I can't investigate.)
-
CodeDom should use whatever the newest installed compiler is. I assume that if you installed VS 2010 you'd have the C# v4.0 compiler and it should work fine. (I haven't, so I can't investigate.)
-
Well, I'd say the same until now. But I've got VC# 2008 (Express) for that, so it's rather not the case.
That's C# 3.0[^] Execute:
dir /b/s %SystemDrive%\windows\microsoft.net\framework\csc.exe
I get:C:\>dir /s/b %SystemDrive%\windows\microsoft.net\framework\csc.exe
C:\windows\microsoft.net\framework\v1.1.4322\csc.exe
C:\windows\microsoft.net\framework\v2.0.50727\csc.exe
C:\windows\microsoft.net\framework\v3.5\csc.exeThe version of csc that comes with .net v3.5 implements C# 3.0. CodeDom should be calling the latest installed version of the compiler.
-
That's C# 3.0[^] Execute:
dir /b/s %SystemDrive%\windows\microsoft.net\framework\csc.exe
I get:C:\>dir /s/b %SystemDrive%\windows\microsoft.net\framework\csc.exe
C:\windows\microsoft.net\framework\v1.1.4322\csc.exe
C:\windows\microsoft.net\framework\v2.0.50727\csc.exe
C:\windows\microsoft.net\framework\v3.5\csc.exeThe version of csc that comes with .net v3.5 implements C# 3.0. CodeDom should be calling the latest installed version of the compiler.
Thanks for your answer.
PIEBALDconsult wrote:
CodeDom should be calling the latest installed version of the compiler.
It should but did not. Anyway I was given a working solution on another forum.
Dictionary<string, string> options = new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } }; Microsoft.CSharp.CSharpCodeProvider cp = new Microsoft.CSharp.CSharpCodeProvider(options);
Obviously, instead of v3.5 you can specify any other supported version.
-
Thanks for your answer.
PIEBALDconsult wrote:
CodeDom should be calling the latest installed version of the compiler.
It should but did not. Anyway I was given a working solution on another forum.
Dictionary<string, string> options = new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } }; Microsoft.CSharp.CSharpCodeProvider cp = new Microsoft.CSharp.CSharpCodeProvider(options);
Obviously, instead of v3.5 you can specify any other supported version.
Huh, good to know.