Project Description
Runtime Hello Worlds is a very simple project demonstrating a number of ways of implementing "Hello, World" in C#/.NET 4.0 in runtime generated and/or bound code.

Mini FAQ:

What is this?

Runtime Hello Worlds is a console app that demonstrates the classic "Hello World!" program implemented by different dynamic and run-time code generation/binding techniques. It shows the technique simply and allows for some rudimentary comparison between them. To be clear, here's the only thing each of these examples is doing, as implemented in plain, ole C#:

   public static void SayHelloWorld()
   {
        Console.WriteLine("Hello, World! (CSharp)");
   }  

Here's the simplest example of what I'm talking about; here's how to use Reflection to call the Console.WriteLine(string) method to output "Hello, World! (Reflection)":
   public static void SayHelloWorld()
   {
        MethodInfo writeLine = typeof(Console).GetMethod("WriteLine", new[] { typeof(string) });
        writeLine.Invoke(null, new[] {"Hello, World! (Reflection)"});
   }

There are 12 methods so far, and some are very similar to others.

Why make this project?

It's really just a demonstration of the techniques.

My original motivation was simply to fully understand all the ways of creating and/or invoking code at run-time in .NET. I had been using many of these techniques, but felt less familiar with them than I should. This project is partly a result of that. It was very illuminating (I found, anyway) to implement them all side-by-side. More illuminating was the process of thinking of which techniques to try implementing and it was very interesting to fail to implement certain techniques.

I hope someone will find them useful as a demonstration or inspiration for solving a problem in a different way.

What are the techniques?

The implemented techniques are:
  • Reflection
  • Reflection.Emit
  • CSharp CodeProvider
  • CSharp CodeDom
  • DynamicMethod
  • DynamicObject
  • Expression compilation
  • Expression visitor
  • Expressions + Reflection.Emit
  • IronPython
  • JScript.NET
  • Jurrasic (JavaScript)

All examples are implemented in their own class, from within one project. I wanted to do this to discover any problems with using all the techniques in a single project (not that I advocate that sort of thing!). I simply find that constraints like that can lead to you learning something. (I was right in this case: turns out that IronPython 2.7 shares some fully-qualified type names with with Expressions, so I got to learn how to use external aliases for references).

There are other techniques that I can add in the future such as
  • Other JavaScript or language implementations if they make sense and can be C# hosted
  • Some method involving native code.
  • Mono.Cecil created code
  • CCI created code

Last edited Aug 22, 2011 at 8:12 AM by ZeroBugBounce, version 6