You are viewing this site over HTTP, which is not secure. Please consider switching to HTTPS instead.

A .NET Snippet Compiler

Lately, I found myself experimenting with Python. I like some of the ideas behind the language, especially the way it handles lists. But the only real use I could come up with is scripting. For that, I now use 'normal' cmd-style batch processing primarily, and looking into Powershell has been on my list for a while. IronPython integrates with the .NET framework, which should enable me to start writing concise, powerful scripts in no-time.

But hey, wait a minute. I'm trying to harness the power of .NET in things like file handling, XML, etc. So why not use VB.NET or C# directly? Can't I write scripts in those languages?

No - not easily, that is. You need Visual Studio to make an executable file for you. And sometimes that's just not what I want. The reason I use scripts for certain tasks is that you're supposed to be able to change things quickly.

I remembered that the .NET Framework allows you to compile code snippets - basically strings containing code. After some searching and fiddling I came up with CodeRunner, a class that compiles and executes code snippets via the command line. It enables you to write a small class along the lines of:

using System;
using System.IO;
 
public class Program
{
  public static void Main()
  {
    DirectoryInfo dirInfo = new DirectoryInfo(".");
 
    Console.WriteLine("Directory of " + dirInfo.FullName);
    foreach (FileInfo f in dirInfo.GetFiles())
    {
      Console.WriteLine(string.Format("{0,-30} {1,10}", f.Name, f.Length));
    }
    Console.WriteLine("END");
  }
}

You could write the same code in VB.NET, of course. The snippet requires a public class called Program containing a static method Main() - that's all.