Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to dynamically use C# 3.5 compiler

How to dynamically use C# 3.5 compiler

Scheduled Pinned Locked Moved C#
csharphelptutorialquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pbalaga
    wrote on last edited by
    #1

    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! :)

    P 1 Reply Last reply
    0
    • P pbalaga

      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! :)

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      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.)

      P 1 Reply Last reply
      0
      • P PIEBALDconsult

        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.)

        P Offline
        P Offline
        pbalaga
        wrote on last edited by
        #3

        Well, I'd say the same until now. But I've got VC# 2008 (Express) for that, so it's rather not the case.

        P 1 Reply Last reply
        0
        • P pbalaga

          Well, I'd say the same until now. But I've got VC# 2008 (Express) for that, so it's rather not the case.

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          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.exe

          The version of csc that comes with .net v3.5 implements C# 3.0. CodeDom should be calling the latest installed version of the compiler.

          P 1 Reply Last reply
          0
          • P PIEBALDconsult

            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.exe

            The version of csc that comes with .net v3.5 implements C# 3.0. CodeDom should be calling the latest installed version of the compiler.

            P Offline
            P Offline
            pbalaga
            wrote on last edited by
            #5

            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.

            P 1 Reply Last reply
            0
            • P pbalaga

              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.

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              Huh, good to know.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups