Welcome to MSDN Blogs Sign in | Join | Help

hello, world... LCG (Lightweight Code Gen) style!

I've finally gotten around to cooking up a post about the the spectrum of late-bound invocations one may make over methods, and it includes a look at a new Whidbey feature called LCG (Lightweight Code Gen). I'll be posting the invocation story very soon, but I figured I'd give a very brief overview of LCG first: LCG provides runtime code generation facilities for emitting global static methods. It looks and feels much like Reflection.Emit but sharpens its focus to just the generation of methods and their IL. It's lighter than RE, and targets scenarios like late-bound invocation, serialization, partial evaluation and runtime code generation. It's contrast to RE looks something like the following:

  • Static methods only.
  • Doesn't incur the overhead of generating new assemblies, modules and types.
  • Generated methods are able to be reclaimed.
  • Has the ability to skip JIT-time visibility checks, given appropriate permissions.

Enough of the overview, on to the traditional hello, world app:

using System;

using System.Reflection;

using System.Reflection.Emit;

 

public class LCGHelloWorld

{

      public static void Main(string[] args)

      {

            DynamicMethod dm = new DynamicMethod("HelloWorld", typeof(void), new Type[] {}, typeof(LCGHelloWorld), false);

            ILGenerator il = dm.GetILGenerator();

 

            il.Emit(OpCodes.Ldstr, "hello, world");

            il.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }));

            il.Emit(OpCodes.Ret);

 

            dm.Invoke(null, null);

      }

}

Notice the lack of Assembly, Module and Type builders. It's as simple as that - cook up a DynamicMethod and fire. You can find the very preliminary msdn style docs here - expect the documentation to solidify closer to the Beta 1 drop. In the meantime, have a play on the Whidbey PDC bits.

Enjoy.

Published Wednesday, March 31, 2004 9:35 PM by joelpob

Comments

# re: Python for the CLR: Jim Hugunin's IronPython paper at PyCON

Thursday, April 01, 2004 12:40 AM by Jason Zander's WebLog

# Fast Python on the CLR

Friday, April 02, 2004 4:49 PM by Extreme RAD from a Trading Desk

# LCG in Whidby

Friday, April 02, 2004 6:05 PM by Andrew Stopford's Weblog

# LCG in Whidby

Saturday, April 03, 2004 5:07 PM by Andrew Stopford's Weblog

# LCG in Whidby

Saturday, April 03, 2004 5:10 PM by Andrew Stopford's Weblog

# Joel's Lightweight Code Gen spells SUWEET for small scripting languages in games.

Saturday, April 10, 2004 2:46 PM by Justin Rogers, DigiTec Web Consultants, LLC.

# New and Notable 40

Friday, April 23, 2004 3:02 PM by Sam Gentile's Blog

# What's new in System.Reflection (and friends)

Wednesday, June 23, 2004 1:21 AM by Joel Pobar's weblog

# What's new in System.Reflection (and friends)

Wednesday, June 23, 2004 1:37 PM by Joel Pobar's weblog

# Groovy baby!

Tuesday, July 13, 2004 4:12 AM by Andrew Stopford's Weblog

# re: August 19 MSDN Webcast: Moving from PHP to ASP.NET

Wednesday, August 18, 2004 7:49 PM by Joe Stagner - Frustrated by Design !

# Groovy baby!

Saturday, November 20, 2004 2:56 PM by Andrew Stopford's Weblog

# Debugging Dynamically Generated Code (Reflection.Emit)

Thursday, February 03, 2005 1:56 PM by Mike Stall's .NET Debugging Blog

# CLR Dynamic languages under the hood (Part 1 of many)

Friday, July 01, 2005 2:44 PM by Joel Pobar's CLR weblog
There seems to be a fair amount of recent press and blog action surrounding the dynamic or “scripting”...

# CLR Dynamic languages under the hood (Part 1 of many)

Friday, July 01, 2005 2:48 PM by Joel Pobar's CLR weblog
There seems to be a fair amount of recent press and blog action surrounding the dynamic or “scripting”...

# Thoughts on automatically generating Test Methods in a Team Developer Edition Test Project

Sunday, April 16, 2006 4:51 PM by BM Bloggers
With my current implementation of the GUITesting framework integrated with Test Projects within VS.Net,...

# DebuggerVisualizer for DynamicMethod (Show me the IL)

Sunday, October 08, 2006 2:59 PM by Haibo Luo's weblog

Have you ever tried DynamicMethod, one of the coolest features in the .NET 2.0? Hope you have; otherwise,

# LCG + Debuggability, and your feedback

Tuesday, November 14, 2006 3:58 PM by Mike Stall's .NET Debugging Blog

I mentioned earlier that you can debug Reflection.Emit code . Unfortunately, Ref.Emit code can't be unloaded

# Famous Quotes » Jason Zander’s WebLog : Python for the CLR: Jim Hugunin’s IronPython …

# Generic Method Invocation with Expression Trees « Solutionizing .NET

# Joel Pobar s CLR weblog hello world LCG Lightweight Code Gen style | Cast Iron Cookware

# Joel Pobar s CLR weblog hello world LCG Lightweight Code Gen style | Outdoor Ceiling Fans

New Comments to this post are disabled
 
Page view tracker