© 2004 Microsoft Corporation. All rights reserved.
The Road to Scripting in the .NET Framework
Within the .NET Framework, the System.CodeDom.Compiler namespace provides code compilers and code generators for C#, Visual Basic®, and JScript. By interacting with the classes in the assembly, you can create an in-memory compiler for the language of choice and programmatically instruct it to compile source code from either a string or a disk file.
      More importantly, the output assembly can be heap-allocated in memory, as well as stored to disk with a given name. If the assembly is generated in memory it is given an auto-generated name that you don't need to know. So, what is it for? Just imagine having a C# file in a folder, double-clicking on it, and having the code automatically compiled on the fly and then run. It's like today's WSH using .NET languages with full access to the .NET Framework.
      The code in shows how to compile a C# file in memory. Although the code is written for C#, it can be easily adapted to work with other languages. Replace the CSharpCodeProvider class with the VBCodeProvider class to run Visual Basic .NET code and with the JScriptCodeProvider class for JScript .NET. The rest of the method is language independent.
      You set some compiler parameters, such as the referenced assemblies, and determine whether the output must be an executable or a library. The method CompileAssemblyFromSource on the compiler class returns a CompilerResults object. This class has a method called CompiledAssembly, which constitutes a reference to the assembly just created.
      C# is not a script language, so C# programs need to have an explicit entry point. A simple line like the following is not a valid C# program:
System.Windows.Forms.MessageBox.Show("Hi there");
A main class and static Main module are necessary for a program to run. These can be considered boilerplate code, however, and you can add them just before compiling. At the same time you can import a few standard namespaces to simplify coding. This code can automatically wrap any simple C# code and make it executable:
   StringBuilder sb = new StringBuilder("");
   sb.Append("using System;");
   •••
   sb.Append("namespace Expoware { class ExpowareApp {");
   sb.Append("public static void Main() {");
   sb.Append(source);
   sb.Append("}}}");
      The final piece of code would look like the following:
   namespace Expoware { class ExpowareApp {
   public static void Main() {
       MessageBox.Show("Hi there");
   }}}
      You don't have to worry about the namespace and the main class. The following code illustrates how you create an instance of the main class in the C# code, retrieve the entry point in the assembly just created, and invoke it.
   Object o = a.CreateInstance("ExpowareApp");
   MethodInfo mi = a.EntryPoint;
   mi.Invoke(o, null);
      Since this is not true C# code, I decided to use a different extension to distinguish this kind C# script from regular C# sources. I used the .csx extension and entered some registry changes to update the shell. By associating the .csx C# script files with csc.exe you can double-click on such files and they will automatically compile and run, just like WSH works today with .vbs and .js files (see Figure B). You can write C# script code for data access, XML, and advanced user interface programming through Windows Forms.

Figure B CSX Project
Figure B CSX Project



MSDN Magazine Blog

MSDN Magazine Right Rail

14 Top Features of Visual Basic 14: The Q&A
Leading off the feature in the January issue of MSDN Magazine is Lucian Wischik’s fantastic look at Visual Basic .NET 14. As Wischik writes, the newes... More...
Wednesday, Jan 7
Big Start to the New Year at MSDN Magazine
Folks, things are hopping over here at MSDN Magazine. We are kicking off the new year with a pair of issues: Our regularly scheduled January issue and... More...
Friday, Jan 2

More MSDN Magazine Blog entries >


Receive the MSDN Flash e-mail newsletter every other week, with news and information personalized to your interests and areas of focus.