Using Reflection Emit

Reflection emit is the process of creating your own types and code at runtime. When you use reflection emit, you create not only a type, but also the members of that type—and compile them on the fly, “emitting” the MSIL code for the new type. Note that this is an advanced technique, and the code here is going to get a little involved. Not many programmers use reflection emit.

To get an idea of the kinds of problems reflection emit can solve, take a look at ch14_08.cs, Listing 14.8. In that example, we'll use a method named CountLoop to count from 1 to 30:

public int CountLoop(int n)
{
  int total = 0;

  for(int loopIndex = 1; loopIndex <= n; loopIndex++)
  {
    total++;
  }

  return total;
}

This method relies on a loop to execute, ...

Get Microsoft® Visual C#® .NET 2003 Kick Start now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.