Welcome to MSDN Blogs Sign in | Join | Help

C# 3.0 and CodeDOM

The CodeDOM is a very handy .NET API which allows you to programatically compile code using the .NET compilers and programatically construct code without just pasting together strings. 

With the new version of the language, we've heard a numer of questions about how to use the CodeDOM with the new compiler.

There are two aspects to the inegration of C#3.0 with CodeDOM:

  • Programatically compiling C# source code: This is supported in .NET3.5.  The existing CodeDOM is augmented with an overload of CSharpCodeProvider which takes a "providerOptions" argument.  This can be used to point CodeDOM at the new .NET Framework 3.5 C# compiler (which supports C#3.0), by passing a "providerOptions" dictionary containing "CompilerVersion" as "v3.5".  This can also be controlled via the .config file, which is done  for example in Orcas ASP.NET websites targeting .NET3.5. 

  • Programatically constructing C#3.0 source code:  There won't be support for the this in the .NET Framework 3.5 CodeDOM that ships with Orcas.  Luckily, very few of the C# 3.0 features need CodeDOM support - since most new features are expressions, and the CodeDOM doesn't go down to the expression level. 
Example of programatically compiling C#3.0 source code:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
class Program
{
    static void Main(string[] args)
    {
        var csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });
        var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" }, "foo.exe", true);
        parameters.GenerateExecutable = true;
        CompilerResults results = csc.CompileAssemblyFromSource(parameters,
        @"using System.Linq;
            class Program {
              public static void Main(string[] args) {
                var q = from i in Enumerable.Rnge(1,100)
                          where i % 2 == 0
                          select i;
              }
            }"
);
        results.Errors.Cast<CompilerError>().ToList().ForEach(error => Console.WriteLine(error.ErrorText));
    }
}

Published Wednesday, July 11, 2007 4:13 PM by LukeH

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: C# 3.0 and CodeDOM

Wednesday, August 08, 2007 12:26 PM by MatthieuMEZIL

I start a POC of a compiler for C# 3.0 extended (with CodeDom).

I want to do a lot of things (http://blog.developpez.com/index.php?blog=121&title=mmcs31&more=1&c=1&tb=1&pb=1) but I only start. You can find my project in codeplex (http://www.codeplex.com/mmcs31).

# Compiling C# 3.0 during runtime

Friday, August 10, 2007 4:08 PM by Ken Brubaker

This post explains how to get the CSharpCodeProvider compile C# 3.0 code. Here's the magic line:...

# Community Convergence XXIX

Monday, August 13, 2007 2:39 AM by Charlie Calvert's Community Blog

There are several good new blogs from members of the Microsoft C# team. Nevertheless, the most important

# CodeDOM na vers

Tuesday, September 04, 2007 2:02 PM by Nuno Filipe Godinho

# re: C# 3.0 and CodeDOM

Wednesday, December 05, 2007 8:06 PM by marcod

Thank you very much, using the alternative of specifying CompilerVersion with value v3.5 on the referred class constructor worked very well.

Unfortunately, the other alternative is needed for another application and specifying the same data on its application config file does not work the same.

In order to paste the exact config section, I copied it from a just created Visual Studio 2008 ASP.NET Web site’s web.config file:

<system.codedom>

 <compilers>

   <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4"

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

         <providerOption name="CompilerVersion" value="v3.5"/>

         <providerOption name="WarnAsError" value="false"/>

   </compiler>

   <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warningLevel="4"

                 type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">

         <providerOption name="CompilerVersion" value="v3.5"/>

         <providerOption name="OptionInfer" value="true"/>

         <providerOption name="WarnAsError" value="false"/>

   </compiler>

 </compilers>

</system.codedom>

I tried also with the sample on Compiler and Language Provider Settings Schema <providerOption> Element (no difference):

<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"

     compilerOptions="/optimize"

     warningLevel="1" />

       <providerOption

         name="CompilerVersion"

         value="3.5" />

 </compilers>

</system.codedom>

What I am missing?

Thank you very much in advance for your help.

# re: C# 3.0 and CodeDOM

Thursday, December 06, 2007 2:44 PM by LukeH

Marco -

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

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

instead of the method used above which directly creates a CSharpCodeProvider.

# re: C# 3.0 and CodeDOM

Wednesday, March 12, 2008 9:21 AM by Dude76

THANKS ! You saved me after 3 days of looking for what's going wrong !

If you don't use specific version 3.5 for compiler, System.Core.dll causes many difficulties and compilation failed !

thx so much !

# re: C# 3.0 and CodeDOM

Wednesday, June 18, 2008 7:55 AM by RunningMile

Looking for a way to generate IL for dynamic methods without creating DynamicAssemblies - need to incorporate high performance scripting capability into our 3D application without having to load new dynamic assembly on every change... we do not need to create new classes, just a rapid way to compile C# script...

# re: C# 3.0 and CodeDOM

Friday, September 19, 2008 1:21 PM by Matt

Echoing what Dude76 said, have been trying this all day. Was missing the compiler version

(my error was saying 'From' undefined, just in case someone else has same error).

Thanks!

# re: C# 3.0 and CodeDOM

Monday, October 20, 2008 5:01 PM by John

Thanks Luke - this tip saved my bacon!

# The source code IS the executable [Releasing CSI, a C# interpreter (with source and tests) for .NET]

Wednesday, January 07, 2009 3:29 AM by Delay's Blog

A few years ago I found myself spending a lot of time writing batch files to perform a variety of relatively

# re: C# 3.0 and CodeDOM

Friday, July 24, 2009 6:32 PM by Nir

Wow, I was searching for hours how I can use collection initializers in code dom. Thank you so much!

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker