Home > Articles > Programming > Windows Programming

Emitting MSIL with Reflection

📄 Contents

  1. The Imminent Potential of Microsoft Intermediate Language (MSIL)
  2. Brief Overview
  3. Emitting Dynamic Assemblies
  4. Creating a "Hello World" Emitter
  5. Summary
  • Print
  • + Share This
Emitting Microsoft Intermediate Language (MSIL) code is an essential precursor to building adaptive software. Paul Kimmel shows how to emit MSIL using Reflection. Reflection.Emit is the foundation for the CodeDom, which can be used to generate Visual Basic .NET or C# code to a file or to a compiled assembly.
This article is adapted from Paul Kimmel's upcoming book, The Visual Basic .NET Developer's Book (Addison-Wesley, scheduled for publication Fall 2002, ISBN 0-672-32407-5).
From the author of

Introduction

Web services sit squarely at the cornerstone of Microsoft's marketing initiative for .NET. Web services offer a promise of shared information, cross-platform application integration, and facilitated workflow solutions. These concepts are targeted at business managers, and businesses should be enthusiastic about this technology. However, among technologists the most animated discussions are on the subject of reflection and emitting.

.NET reflection supports emitting Microsoft Intermediate Language (MSIL)—an intermediate language between assembly and a .NET language like C#. The System.Reflection.Emit namespace contains classes that support emitting dynamic assemblies that contain MSIL, and on top of the System.Reflection.Emit namespace is the CodeDom technology (the System.CodeDom and System.CodeDom.Compiler namespaces). The CodeDom allows programmers to emit and compile code such as Visual Basic .NET and C#.

Why do programmers find this technology so compelling? I think the reason is that it gets the imagination and the creative juices flowing. We imagine creating software that's smart, that evolves—dubbed adaptive software. Perhaps the software evolves based on usage patterns or the acquisition of data, but it evolves.

It may be too early to know for sure whether .NET will advance the capability of smart programs, but the notion of adaptive software is a compelling conversation among techno-weenies (a Tom Clancy term for geeks).

One thing for sure is that .NET has a lot to offer. There's a promise of integrated business solutions using open standards, and for technologists a promise of access to cool technologies. In this short article, I'll introduce what Harry Pierson (of Microsoft's National Technology Team) calls writing code generators and I call writing emitters: how classes in the Reflection.Emit namespace can be used to write MSIL at runtime.

  • + Share This
  • 🔖 Save To Your Account