Reflection: Browsing Code Information, Part 3
Up to this point in this series, the reflection services have been used to read type information at runtime. It is also possible to have your code build code. Dynamically building and running code is used in a number of different places throughout the .NET Framework. Two such examples are the Regex (regular expression) class and the XmlSerializer class. The tools are available for you to generate your own code by using code.
This section first discusses a simple "Hello World!" sample in which a single function can generate code for all of the languages supported. This function builds a code tree and then hands the tree over to the code generation APIs. This section will then delve into a more real-world sample where you gain improvement over the brute force method of multiplying matrixes. Finally, this section will discuss Reflection.Emit and dynamically generating IL code.
Code Document Object Model (CodeDom)
The complete source for this sample is located in CodeCom\GenerateCode. The source that is presented here is shown in Listing 12.
Listing 1CodeProvider Initialization
public static void HelloWorldCode(CodeDomProvider provider, StreamWriter output) { // Generate code ICodeGenerator cg = provider.CreateGenerator(output); CodeGeneratorOptions op = new CodeGeneratorOptions();
The function takes two generic arguments. The first is a CodeDomProvider, which is the base class for all CodeDomProviders. Currently, only two CodeDomProviders exist: one for VB (VBCodeProvider) and one for C# (CsharpCodeProvider). Eventually, many other languages will be supported. If you pass a VBCodeProvider to this method, then VB code will be generated. If you pass CsharpCodeProvider, then C# code will be generated. The output Stream argument is a generic Stream for the destination of the generated code. You will generate some comments and wrap a namespace around the code. The code listing continues with 2.
Listing 2Comments and Namespace
// Generate the comments at the beginning of the function CodeCommentStatement comment = new CodeCommentStatement("Code to generate Hello World"); cg.GenerateCodeFromStatement(comment, output, op); comment = new CodeCommentStatement("First generate the namespace."); cg.GenerateCodeFromStatement(comment, output, op); // The namespace CodeNamespace codeNamespace = new CodeNamespace("HelloWorld"); // The namespace CodeNamespaceImport import = new CodeNamespaceImport("System"); codeNamespace.Imports.Add(import);
Some comments are inserted at the beginning of the file, and a namespace is wrapped around the code. Notice that these classes are all generic in nature, making it easy to generate code in many different languages.
Listing 3Main Entry Point
// The class is named with a unique name CodeTypeDeclaration mainClass = new CodeTypeDeclaration("HelloWorldMain"); // Add the class to the namespace codeNamespace.Types.Add(mainClass); // Set up the Main function CodeEntryPointMethod main = new CodeEntryPointMethod(); CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression("System.String []", "args"); comment = new CodeCommentStatement("Output the greeting on the Console"); main.Statements.Add(comment);
This section of code generates an entry point for the generated code. In C#, this should look something like this: "void Main(strings[] args)". The listing continues with a variable declaration that will contain the message to be written. Next, you declare a variable to hold the message in Listing 4.
Listing 4Declare a Variable
CodeVariableDeclarationStatement variable = new CodeVariableDeclarationStatement(typeof(string), "message", new CodePrimitiveExpression("Hello World!")); main.Statements.Add(variable);
A string variable called message is declared and initialized to "Hello World!". In Listing 5, you finish things up and generate the code.
Listing 5Call Console.WriteLine to Output a Message to the Console
CodeVariableReferenceExpression[] arg = new CodeVariableReferenceExpression[1]; arg[0] = new CodeVariableReferenceExpression(variable.Name); CodeMethodReferenceExpression type = new CodeMethodReferenceExpression(); type.MethodName = "Console"; CodeMethodInvokeExpression methodCall = new CodeMethodInvokeExpression(type,"WriteLine",arg); main.Statements.Add(methodCall); mainClass.Members.Add(main); cg.GenerateCodeFromNamespace(codeNamespace, output, op); }
Here, you pass the variable message to Console.WriteLine, effectively writing "Hello World!" to the Console. After this last bit of code is generated, the GenerateCodeFromNamespace method on ICodeGenerator interface is called to generate the code for the tree that you have just constructed.
The code that drives this function is in the Main entry point for the sample. Listing 6 shows a portion of this code.
Listing 6Generating Code and Executing the Code
static void Main(string[] args) { CSharpCodeProvider csprovider = new CSharpCodeProvider(); string filename = "HelloWorld"; Stream s = File.Open(filename + ".cs", FileMode.Create); StreamWriter t = new StreamWriter(s); GenerateCode.HelloWorldCode(csprovider, t); t.Close(); s.Close(); GenerateCode.CompileAndExecute(csprovider, filename + ".cs", filename + "cs.exe"); VBCodeProvider vbprovider = new VBCodeProvider(); s = File.Open(filename + ".vb", FileMode.Create); t = new StreamWriter(s); GenerateCode.HelloWorldCode(vbprovider, t); t.Close(); s.Close(); GenerateCode.CompileAndExecute(vbprovider, filename + ".vb", filename + "vb.exe"); }
This listing illustrates driving code generation for two different languages. As soon as the interface (CodeProvider) is available for other languages, this function could be extended. The methodology is similar in each section (CSharp and VB). First, a file is created that contains the source to be compiled. Next, the code is generated, and then the code is compiled and executed. Listings 7 and 8 show the code that is generated for this sample.
Listing 7C# Generated Code
// Code to generate Hello World! and "any" language // First generate the namespace. namespace HelloWorld { using System; public class HelloWorldMain { public static void Main() { // Output the greeting on the Console string message = "Hello World!"; Console.WriteLine(message); } } }
Listing 8 shows the VB code.
Listing 8VB-Generated Code
'Code to generate Hello World! and "any" language 'First generate the namespace. Imports System Namespace HelloWorld Public Class HelloWorldMain Public Shared Sub Main() 'Output the greeting on the Console Dim message As String = "Hello World!" Console.WriteLine(message) End Sub End Class End Namespace
Except for syntax differences, the code looks the same. That is the point.
Compiling Code for Multiple Languages
Just as you can generate code for multiple languages, you can also compile code for all languages for which you have a specific implementation of the CodeProvider interface. Again, a short example will help you realize how to accomplish this. This sample will be illustrated in Listings 911. The full source for this sample is in the CodeDom\HelloWorld directory. Listing 9 shows the common code used to compile VB and C# code.
Listing 9Building a Generic Function to Compile and Run Code
public static void CompileAndRun(CodeDomProvider provider, string source, string target) { // Now use the CodeProvider interface CompilerParameters param = new CompilerParameters(null, target, true); param.GenerateExecutable = true; ICodeCompiler cc = provider.CreateCompiler(); CompilerResults cr = cc.CompileAssemblyFromSource(param, source); System.Collections.Specialized.StringCollection output = cr.Output; foreach(string s in output) { Console.WriteLine(s); } if(cr.Errors.Count != 0) { CompilerErrorCollection es = cr.Errors; foreach(System.CodeDom.Compiler.CompilerError e in es) { Console.WriteLine(e.ToString()); } }
Here you just build up the compiler parameters (in this case, just two). The first compiler parameter is the name of the file (assembly) to which to output the compiled results. The second compiled parameter is a flag indicating that you want a standalone executable as opposed to a library. Based on the passed in CodeDomProvider, you create an ICodeCompiler interface. This interface compiles the code and generates the errors if any exist. If errors are present, then you print them. If no errors are present, then you move on to the code in Listing 10.
Listing 10Running Generically Compiled Code
else { // Set ApplicationBase to the current directory AppDomainSetup info = new AppDomainSetup(); info.ApplicationBase = "file:\\\\" + System.Environment.CurrentDirectory; // Create an application domain with null evidence AppDomain dom = AppDomain.CreateDomain("HelloWorld", null, info); dom.ExecuteAssembly(target); // Clean up by unloading the application domain AppDomain.Unload(dom); } }
This is standard boilerplate code that creates an AppDomain and loads and runs the compiled code in that AppDomain. The driver for this routine simply generates the source and creates the CodeDomProvider. This is illustrated in Listing 11.
Listing 11Driver for Testing Various CodeDom Compilers
static void Main(string[] args) { string target = "HelloWorld.exe"; string [] source = new string[1]; source[0] = @"using System; namespace HelloWorld { /// <summary> /// Summary description for HelloWorldMain. /// </summary> class HelloWorldMain { static void Main(string[] args) { Console.WriteLine(""Hello World!""); } } }"; CSharpCodeProvider cscp = new CSharpCodeProvider(); Compiler.CompileAndRun(cscp, source[0], target); source[0] = @"Imports System Namespace SimpleHelloWorld Public Class SimpleHelloWorld 'Run the application Shared Sub Main() Console.WriteLine(""Hello World from VB!"") End Sub End Class End Namespace "; VBCodeProvider vbscp = new VBCodeProvider(); Compiler.CompileAndRun(vbscp, source[0], target); }
This code initializes a string with the source to be compiled, creates a CodeDomProvider, and passes that information to the compiler code shown in Listings 911. The result looks like this:
Hello World! Hello World from VB!
This is a convenient method for dynamically generating and compiling code in a generic fashion.
A specialized compiler is available only in the Microsoft.CSharp namespace that allows the user to compile multiple sources at a time. Instead of a single string containing the source, an array of strings is passed to the interface to be compiled. This is handy, but so far, only a C# version of it is available.
Matrix Multiplication
Now you will generate some code that actually does some work. The Regex class uses dynamically generated code to increase performance, and so does XmlSerializer. In the managed code world, a large body of code is being generated that uses templates to the extreme. The idea is that the compiler is used to help optimize the code. Why can't the same thing be done with managed code?
Eric Gunnerson presented a great example of how dynamically generated code can rival the performance of a custom solution with his polynomial evaluation code. Presented next is a matrix multiplication scheme that shows similar performance gains. The source for the code presented in Listings 1215 is located in the CodeDom\MatrixMultiplication directory.
Just for reference, Listing 12 shows the brute force method of multiplying two matrixes.
Listing 12Brute Force Matrix Multiplication
public override double [,] Multiply() { // Initialize the result array for(int i = 0; i < A.GetLength(0); i++) for(int j = 0; j < B.GetLength(1); j++) C[i,j] = 0.0; // Loop over the rows of A for(int i = 0; i < A.GetLength(0); i++) // Loop over the columns of A and rows of B for(int j = 0; j < A.GetLength(1); j++) { // Loop over the columns of B for(int k = 0; k < B.GetLength(1); k++) C[i,k] += A[i,j] * B[j,k]; } return C; }
The code for Listing 12 performs reasonably well. It will be used as a baseline for measuring how this multiplication can be improved. On my machine, Table 1 shows some performance numbers for multiplying two square N x N matrixes.
Table 1Direct Matrix Multiplication
N |
Multiplications/Second |
10 |
3,200 |
20 |
450 |
50 |
30 |
100 |
4 |
These were respectable results. Now see if you can improve things with some dynamically generated code. You would mainly be focusing on unrolling the loops involved with matrix multiplication. You could apply other algorithms such as Strassen's divide and conquer algorithm, but to show the benefits of dynamic code generation, just focus on loop unrolling.
Listing 13 shows how to generate code at runtime to multiply two arrays. This listing builds on the information presented in the previous section on CodeDom.
Listing 13Matrix Multiplication Using Dynamic Code Generation
private void GenerateCode() { string filename = "mmf"; Stream s = File.Open(filename + ".cs", FileMode.Create); StreamWriter t = new StreamWriter(s); // Generate code in C# CSharpCodeProvider provider = new CSharpCodeProvider(); ICodeGenerator cg = provider.CreateGenerator(t); CodeGeneratorOptions op = new CodeGeneratorOptions(); // Generate the comments at the beginning of the function CodeCommentStatement comment = new CodeCommentStatement("Matrix Multiplication (Fixed)"); cg.GenerateCodeFromStatement(comment, t, op); // The class is named with a unique name string className = "__MatrixMultiplyFixed"; CodeTypeDeclaration matrixClass = new CodeTypeDeclaration(className); // The class implements IPolynomial matrixClass.BaseTypes.Add("MatrixMultiplication.IMatrixMultiply"); // Set up the Multiply function CodeMemberMethod multiplyMethod = new CodeMemberMethod(); multiplyMethod.Name = "Multiply"; multiplyMethod.Parameters.Add( new CodeParameterDeclarationExpression("double [,]", "A")); multiplyMethod.Parameters.Add( new CodeParameterDeclarationExpression("double [,]", "B")); multiplyMethod.Parameters.Add( new CodeParameterDeclarationExpression("double [,]", "C")); // workaround for bug below... multiplyMethod.ReturnType = new CodeTypeReference("public void"); // BUG: This doesn't generate "public"; it just leaves // the attribute off of the member... multiplyMethod.Attributes |= MemberAttributes.Public; StringBuilder str = new StringBuilder(); str.Append("{"); // Loop over the rows of A for(int i = 0; i < A.GetLength(0); i++) { // Loop over the columns of A and rows of B for(int j = 0; j < A.GetLength(1); j++) { // Loop over the columns of B for(int k = 0; k < B.GetLength(1); k++) C[i,k] += A[i,j] * B[j,k]; } str.Append("{"); for(int j = 0; j < B.GetLength(1); j++) { if(j < A.GetLength(0) - 1) str.Append(string.Format("{0},\r\n",C[i,j])); else str.Append(string.Format("{0}",C[i,j])); } if(i < A.GetLength(0) - 1) str.Append("},\r\n"); else str.Append("}"); } str.Append("}"); CodeSnippetTypeMember variable = new CodeSnippetTypeMember( string.Format( "static readonly double [,] result = new double[{0},{1}]\r\n{2};", A.GetLength(0), B.GetLength(1), str.ToString())); matrixClass.Members.Add(variable); CodeAssignStatement assignment = new CodeAssignStatement(); assignment.Left = new CodeVariableReferenceExpression("C"); assignment.Right = new CodeVariableReferenceExpression("result"); multiplyMethod.Statements.Add(assignment); matrixClass.Members.Add(multiplyMethod); cg.GenerateCodeFromType(matrixClass, t, op); t.Close(); s.Close(); // Now use the CodeProvider interface CSharpCodeProvider cscp = new CSharpCodeProvider(); CompilerParameters param = new CompilerParameters(null, null, true); param.GenerateInMemory = true; param.IncludeDebugInformation = false; param.CompilerOptions = "/optimize+"; param.ReferencedAssemblies.Add("MatrixMultiplication.exe"); ICodeCompiler cc = cscp.CreateCompiler(); CompilerResults cr = cc.CompileAssemblyFromFile(param, filename + ".cs"); System.Collections.Specialized.StringCollection output = cr.Output; foreach(string outputSting in output) { Console.WriteLine(outputSting); } if(cr.Errors.Count != 0) { CompilerErrorCollection es = cr.Errors; foreach(System.CodeDom.Compiler.CompilerError e in es) { Console.WriteLine(e.ToString()); } } else { matrixMultiplyInterface = (IMatrixMultiply)cr.CompiledAssembly.CreateInstance(className); } }
This code is not unlike the simple code that generated the "Hello World" message in Listings 16. Now see how it performs. The performance numbers have been compiled in Table 2.
Table 2 Fixed Matrix Multiplication
N |
Multiplications/Second |
10 |
10,876,658 |
20 |
10,794,473 |
50 |
10,976,948 |
100 |
10,638,297 |
Wow! The results seem to be independent of the size of the array. And talk about fast! As you suspected, this is too good to be true. Essentially, the two matrixes were multiplied, and the results were statically recorded. Now whenever a user asks to multiply those two matrixes, you can just return the static result array. The code that is generated looks like that in Listing 14.
Listing 14Fixed Matrix Multiplication-Generated Code
public class __MatrixMultiplyFixed : MatrixMultiplication.IMatrixMultiply { static readonly double [,] result = new double[10,10] {{1.88956889330368, 1.30658585741975, 1.31508360075735, . . . 1.43246374777113, 1.80918436134982, 1.19113607833798}}; public void Multiply(double [,] A, double [,] B, double [,] C) { C = result; } }
If you have a computationally intense function or expression to evaluate, and you only need to evaluate it once and use the results many times, this might be one method to employ. You can achieve the same result with a global static variable and possibly a flag, but this is just another means to an end.
Listing 15 shows the code to dynamically generate code to multiply two matrixes, unrolling the loop to increase performance.
Listing 15Matrix Multiplication Unrolling Loops at Runtime
private void GenerateCode() { string filename = "mml"; Stream s = File.Open(filename + ".cs", FileMode.Create); StreamWriter t = new StreamWriter(s); // Generate code in C# CSharpCodeProvider provider = new CSharpCodeProvider(); ICodeGenerator cg = provider.CreateGenerator(t); CodeGeneratorOptions op = new CodeGeneratorOptions(); // Generate the comments at the beginning of the function CodeCommentStatement comment = new CodeCommentStatement( "Matrix Multiplication (Loop)"); cg.GenerateCodeFromStatement(comment, t, op); // The class is named with a unique name string className = "__MatrixMultiplyLoop"; CodeTypeDeclaration matrixClass = new CodeTypeDeclaration(className); // The class implements IPolynomial matrixClass.BaseTypes.Add("MatrixMultiplication.IMatrixMultiply"); // Set up the Multiply function CodeMemberMethod multiplyHelperMethod = new CodeMemberMethod(); multiplyHelperMethod.Name = "_Multiply"; multiplyHelperMethod.Parameters.Add( new CodeParameterDeclarationExpression(typeof(double [,]), "A")); multiplyHelperMethod.Parameters.Add( new CodeParameterDeclarationExpression(typeof(double [,]), "B")); multiplyHelperMethod.Parameters.Add( new CodeParameterDeclarationExpression(typeof(int), "i")); multiplyHelperMethod.Parameters.Add( new CodeParameterDeclarationExpression(typeof(int), "j")); multiplyHelperMethod.ReturnType = new CodeTypeReference(typeof(double)); { CodeBinaryOperatorExpression plus; CodeBinaryOperatorExpression lastplus; plus = new CodeBinaryOperatorExpression(); plus.Operator = CodeBinaryOperatorType.Add; CodeMethodReturnStatement returnStatement = new CodeMethodReturnStatement(plus); CodeBinaryOperatorExpression multiply; multiply = new CodeBinaryOperatorExpression(); multiply.Operator = CodeBinaryOperatorType.Multiply; multiply.Right = new CodeSnippetExpression("A[i,0]"); multiply.Left = new CodeSnippetExpression("B[0,j]"); plus.Left = multiply; lastplus = plus; for(int k = 1; k < A.GetLength(1); k++) { multiply = new CodeBinaryOperatorExpression(); multiply.Operator = CodeBinaryOperatorType.Multiply; multiply.Right = new CodeSnippetExpression( string.Format("A[i,{0}]",k)); multiply.Left = new CodeSnippetExpression( string.Format("B[{0},j]",k)); if(k < A.GetLength(1) - 1) { plus = new CodeBinaryOperatorExpression(); plus.Operator = CodeBinaryOperatorType.Add; lastplus.Right = plus; plus.Left = multiply; } else { lastplus.Right = multiply; } lastplus = plus; } multiplyHelperMethod.Statements.Add(returnStatement); } matrixClass.Members.Add(multiplyHelperMethod); // Set up the Multiply function CodeMemberMethod multiplyMethod = new CodeMemberMethod(); multiplyMethod.Name = "Multiply"; multiplyMethod.Parameters.Add( new CodeParameterDeclarationExpression(typeof(double [,]), "A")); multiplyMethod.Parameters.Add( new CodeParameterDeclarationExpression(typeof(double [,]), "B")); multiplyMethod.Parameters.Add( new CodeParameterDeclarationExpression(typeof(double [,]), "C")); // workaround for bug below... multiplyMethod.ReturnType = new CodeTypeReference("public void"); // BUG: This doesn't generate "public"; it just leaves // the attribute off of the member... multiplyMethod.Attributes |= MemberAttributes.Public; // Loop over the rows of A for(int i = 0; i < A.GetLength(0); i++) { // Loop over the columns of B for(int j = 0; j < B.GetLength(1); j++) { CodeAssignStatement assignment = new CodeAssignStatement(); assignment.Left = new CodeArrayIndexerExpression( new CodeVariableReferenceExpression("C"), new CodePrimitiveExpression(i), new CodePrimitiveExpression(j)); assignment.Right = new CodeMethodInvokeExpression( null, "Multiply", new CodeVariableReferenceExpression("A"), new CodeVariableReferenceExpression("B"), new CodePrimitiveExpression(i), new CodePrimitiveExpression(j)); multiplyMethod.Statements.Add(assignment); } } matrixClass.Members.Add(multiplyMethod); cg.GenerateCodeFromType(matrixClass, t, op); t.Close(); s.Close(); string target = "mml.dll"; // Now use the CodeProvider interface CSharpCodeProvider cscp = new CSharpCodeProvider(); CompilerParameters param = new CompilerParameters(null, null, true); param.GenerateInMemory = true; param.IncludeDebugInformation = false; param.CompilerOptions = "/optimize+"; param.ReferencedAssemblies.Add("MatrixMultiplication.exe"); ICodeCompiler cc = cscp.CreateCompiler(); CompilerResults cr = cc.CompileAssemblyFromFile(param, filename + ".cs"); System.Collections.Specialized.StringCollection output = cr.Output; foreach(string outputSting in output) { Console.WriteLine(outputSting); } if(cr.Errors.Count != 0) { CompilerErrorCollection es = cr.Errors; foreach(System.CodeDom.Compiler.CompilerError e in es) { Console.WriteLine(e.ToString()); } } else { matrixMultiplyInterface = (IMatrixMultiply)cr.CompiledAssembly.CreateInstance(className); } }
This code still uses CodeDom to build the matrix multiplication code, but the code generation has been fixed so that the matrix multiplication occurs with every matrix. This code truly unrolls the loops when doing a matrix multiplication.
A sample of the code that this code generates is shown in Listing 16.
Listing 16Matrix Multiplication with Unrolled Loops
// Matrix Multiplication (Loop) public class __MatrixMultiplyLoop : MatrixMultiplication.IMatrixMultiply { private System.Double _Multiply(System.Double[,] A, System.Double[,] B, int i, int j) { return ((B[0,j] * A[i,0]) + ((B[1,j] * A[i,1]) + ((B[2,j] * A[i,2]) + ((B[3,j] * A[i,3]) + ((B[4,j] * A[i,4]) + ((B[5,j] * A[i,5]) + ((B[6,j] * A[i,6]) + ((B[7,j] * A[i,7]) + ((B[8,j] * A[i,8]) + (B[9,j] * A[i,9])))))))))); } public void Multiply(System.Double[,] A, System.Double[,] B, System.Double[,] C) { C[0, 0] = _Multiply(A, B, 0, 0); C[0, 1] = _Multiply(A, B, 0, 1); C[0, 2] = _Multiply(A, B, 0, 2); . . . C[9, 7] = _Multiply(A, B, 9, 7); C[9, 8] = _Multiply(A, B, 9, 8); C[9, 9] = _Multiply(A, B, 9, 9); } }
Table 3 shows the performance of this dynamically generated code.
Table 3Matrix Multiplication With Unrolled Loops
N |
Multiplications/Second |
10 |
8,260 |
20 |
1,080 |
50 |
67 |
100 |
5 |
Comparing these results with Table 1, you can see that until you get to multiplying two 100 [ts] 100 matrixes, unrolling the loops seems to increase performance about two fold.
NOTE
In putting these samples together, I ran into some obstacles that under normal circumstances would only occur because the code was being generated by code.
For instance, one problem I ran into was that the line lengths became too long as I generated code. The C# compiler limits the line length to 2,046 characters. I would not normally type 2,046 characters on a line, so this case would most likely occur only with code generating code. This is a line-length limit, not a statement length limit. I can break the 2,046 characters into two lines (not two statements) to make the compiler happy again.
Another problem that I ran into was that when generating the last sample that unrolled the loops, I initially put all of the code into one method. When I got to run the 100 [ts] 100 matrix multiplication test, I noticed that the code file that was generated was tens of megabytes in size, and it exhausted my machine's virtual memory to compile it. In addition, even when multiplying smaller arrays, the dynamically generated code ran almost twice as slow as the brute force method.
I wanted to see why the generated code was so big. Multiplying two 100 [ts] 100 matrixes with the loops unrolled would have 10,000 statements each with 100 multiplication terms in it. I had to think of a better way.
My solution was to create a helper function that performed the multiplication for each term. I still had 10,000 statements, but each statement called a function. That function had 100 multiplication terms in it. This reduced the size of the code generated for a 100 [ts] 100 multiplication to about 450Kb and increased performance as well.
The moral of the story is that the CLR is not friendly toward large methods. Even though I had the extra overhead of calling a function, it dramatically increased the performance of the overall function.
Dynamically generating code, although somewhat difficult to work with, is a valid way to increase the performance of certain classes of functions.
Without reflection, it would not be possible to generate and run code as was done in the previous examples. Reflection can be used to examine and to generate type information at runtime.
Directly Generating IL (Reflect.Emit)
The idea behind Reflect.Emit is much the same as with the CodeDom model. With Reflection.Emit, you are just generating code at a much lower level code (IL) than with the CodeDom. A simple Hello World sample is included in Listing 17. The full source is in the Emit directory.
Listing 17Using Reflection.Emit to Generate a Hello World Program
static Type GenerateCode() { AppDomain currentDomain = AppDomain.CurrentDomain; // Create new assembly in the current AppDomain AssemblyName assemblyName = new AssemblyName(); assemblyName.Name = "HelloAssembly"; // Create AssemblyBuilder AssemblyBuilder assemblyBuilder = currentDomain.DefineDynamicAssembly( assemblyName, AssemblyBuilderAccess.Run); // Create ModuleBuilder ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("HelloModule"); // Create TypeBuilder TypeBuilder typeBuilder = moduleBuilder.DefineType("HelloClass", TypeAttributes.Public); // Create MethodBuilder MethodBuilder methodBuilder = typeBuilder.DefineMethod("HelloWorld", MethodAttributes.Public, null, null); // Create MSIL generator ILGenerator msil = methodBuilder.GetILGenerator(); // Generate code msil.EmitWriteLine("Hello World!"); msil.Emit(OpCodes.Ret); // Return created type return typeBuilder.CreateType(); } static void Main(string[] args) { try { Type t = GenerateCode(); object o = Activator.CreateInstance(t); MethodInfo mi = t.GetMethod("HelloWorld"); Console.WriteLine(mi.Invoke(o, null)); } catch(Exception e) { Console.WriteLine(e); } }
The code creates an Assembly called HelloAssembly in the current AppDomain. Notice that a culture, version, or signing information is not assigned to this Assembly that would make the AssemblyName a "full" AssemblyName. An AssemblyBuilder is created with an access permission of Run. The other possibilities are RunAndSave and Save. The AssemblyBuilder is used to create a ModuleBuilder, the ModuleBuilder is used to create a TypeBuilder, and the TypeBuilder is used to create a MethodBuilder. MethodBuilder can create ILGenerator. With the ILGenerator, you can emit IL code. For this sample, you just emit enough code to return "Hello World!" from the "HelloWorld" method. After all of the IL code is emitted, a Type can be created and returned. With this Type, you can do a late-bound call on the "HelloWorld" method and print the string that is returned.
Reflection.Emit obviously has enormous possibilities for dynamically generating code. It is potentially easier to use than CodeDom and has less overhead. Of course, you need to know IL, which might make it harder to use this class. The RegEx and Jscript libraries are built upon Reflection.Emit to boost performance much the same as with the matrix multiplication sample that was illustrated, with the added bonus that using Reflection.Emit has lower overhead.