The 3.51 .Net run-time compiler supports C# 3.0 and Stack Overflow gets two thumbs up!

When I’m dealing with a tough programming problem my best resources are Google, coworkers and friends, and books; usually in that order. I’ve just discovered a new resource and it goes directly to the top of the list. Stack Overflow is a new website where programmers ask questions and offer answers. I wasn’t expecting much because almost all programming advice websites are disappointing. You’ll come across them when Googling a specific question and when you get there they bombard you with annoying ads while the answer, if it exists at all, is hidden behind a pay-per-view wall. Stack Overflow’s creators, Jeff Atwood and Joel Spolsky saw an opportunity to do something about it and built a site with ideas drawn from forums, social networks and even Ebay. Programmers are rewarded for asking good questions and giving good answers. Site design is simple and effective. Ask a question and while you’re in the middle of typing, before you’ve even finished, Stack Overflow will offer answers to similar questions already asked. Users rate the questions and answers and the best ones perculate to the top. It really works.

Last night I posted my first question to Stack Overflow: Does the .Net run-time compiler support C# 3.0? . Within 15 minutes I got three useful, correct answers. This was after 2:00am central time. I am impressed and Stack Overflow is highly recommended.

The short answer to my question is .Net defaults to the old (2.0) compiler version and you have to use a different class to get the current version. The corrected code example follows:

using System;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Collections.Generic;
using Microsoft.CSharp;

namespace ML.ExtTest
  {
  class Program
    {
    static void CompileAndGo( string code )
      {
      CompilerParameters cp = new CompilerParameters();
      cp.GenerateExecutable = false;
      cp.GenerateInMemory = true;

      CodeDomProvider provider = new CSharpCodeProvider( new Dictionary { { "CompilerVersion", "v3.5" } } );

      CompilerResults compilerResults = provider.CompileAssemblyFromSource( cp, code );

      if( compilerResults.Errors.HasErrors )
        {
        foreach( CompilerError error in compilerResults.Errors )
          {
          Console.WriteLine( "COMPILER ERROR: " + error.ErrorText );
          }
        }
      else
        {
        Type testClassType = compilerResults.CompiledAssembly.GetType( "TestClass" );

        testClassType.InvokeMember( String.Empty, BindingFlags.CreateInstance, null, null, null );
        }
      }

    static void Main()
      {
      string code =
        "using System;" +
        "class TestClass" +
        "  {" +
        "  public TestClass()" +
        "    {" +
        "    string[] message = { \"Success!\" };" +
        "" +
        "    foreach( string str in message )" +
        "      {" +
        "      Console.WriteLine( str );" +
        "      }" +
        "    }" +
        "  }";

      Console.WriteLine( "This one works:" );
      CompileAndGo( code );

      Console.WriteLine();

      Console.WriteLine( "Changing 'string' to 'var' works too:" );
      CompileAndGo( code.Replace( "string str", "var str" ) );
      }
    }
  }
Posted by David Stafford • November 13, 2008 • Posted in: Uncategorized

Leave a Reply