Welcome to MSDN Blogs Sign in | Join | Help

hello, world... Reflection.Emit style!

Before I head home, I thought I'd post up your typical “Hello, World” style app in Reflection.Emit. ~20 lines of C# code, not too bad for code generation of a Console.WriteLine. For those who don't know what Reflection.Emit is, or what it can do, have a look here here and here for starters.

using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;

public class EmitHelloWorld
{
      static void Main(string[] args)
      {
            // create a dynamic assembly and module 
            AssemblyName assemblyName = new AssemblyName(); 
            assemblyName.Name = "HelloWorld"; 
            AssemblyBuilder assemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
            ModuleBuilder module; 
            module = assemblyBuilder.DefineDynamicModule("HelloWorld.exe"); 
      
            // create a new type to hold our Main method
            TypeBuilder typeBuilder = module.DefineType("HelloWorldType", TypeAttributes.Public | TypeAttributes.Class);
            
            // create the Main(string[] args) method
            MethodBuilder methodbuilder = typeBuilder.DefineMethod("Main", MethodAttributes.HideBySig | MethodAttributes.Static | MethodAttributes.Public, typeof(void), new Type[] { typeof(string[]) });
            
            // generate the IL for the Main method
            ILGenerator ilGenerator = methodbuilder.GetILGenerator();
            ilGenerator.EmitWriteLine("hello, world");
            ilGenerator.Emit(OpCodes.Ret);
 
            // bake it
            Type helloWorldType = typeBuilder.CreateType();
 
            // run it
            helloWorldType.GetMethod("Main").Invoke(null, new string[] {null});
 
            // set the entry point for the application and save it
            assemblyBuilder.SetEntryPoint(methodbuilder, PEFileKinds.ConsoleApplication);
            assemblyBuilder.Save("HelloWorld.exe");
      }
}

Published Wednesday, January 21, 2004 9:14 PM by joelpob

Comments

# Reflection.Emitting an Existing Class?

Wednesday, January 21, 2004 10:03 PM by Steven M. Cohn's WebLog

# Reflection.Emitting an Existing Class?

Thursday, January 22, 2004 7:27 AM by Steven M. Cohn's WebLog

# PDB

Thursday, January 22, 2004 9:19 AM by Dejan Jelovic
How about providing us with a tutorial on using System.Diagnostics.SymbolStore?

# re: hello, world... Reflection.Emit style!

Wednesday, March 31, 2004 9:39 PM by grug

# re: hello, world... Reflection.Emit style!

Thursday, April 22, 2004 4:43 PM by Peter Evans
How about providing scenarios description of how you would really want to use System.Reflection features?

I've read "Generative Programming: Methods, Tools, and Applications" by Czarnecki and Eisenecker and it sounds like generics will be equivalent to Constrained genericty and F-Bounded polymorphism.

What I'd like to know more about is some of the other kinds of generative techinques mentioned in this book and how they map to the .NET reflections framework. Specifically, Chapter 8 and 9 concepts and-or implementations of various uses of reflective features.
I'd also like to know more about using attributes in conjuction with the JIT and the reflections libraries. Thanks for any future posts you make about these topics and how they may or may not intersect with Reflection, CLI/compiler features, or other core .NET libraries.

# re: hello, world... Reflection.Emit style!

Tuesday, April 27, 2004 9:27 PM by Joel Pobar
Peter,

Sounds like a good plan. We're actually cooking up a document internally that describes patterns, practices, scenario's, perf and other interesting Reflection stories. Hopefully we can publish this externally in some fashion.

# Reflection Emit

Thursday, June 03, 2004 7:19 PM by Anatoly Lubarsky Blog

# Reflection.Emit

Thursday, June 03, 2004 7:21 PM by Anatoly Lubarsky Blog

# Debugging Dynamically Generated Code (Reflection.Emit)

Thursday, February 03, 2005 1:56 PM by Mike Stall's .NET Debugging Blog
New Comments to this post are disabled
 
Page view tracker