CHAPTER 18 UNDERSTANDING CIL AND THE ROLE OF DYNAMIC ASSEMBLIES
// Create HelloWorld object and call the correct ctor.
Console.Write("-> Enter message to pass HelloWorld class: ");
string msg = Console.ReadLine();
object[] ctorArgs = new object[1];
ctorArgs[0] = msg;
object obj = Activator.CreateInstance(hello, ctorArgs);
// Call SayHello and show returned string.
Console.WriteLine("-> Calling SayHello() via late binding.");
MethodInfo mi = hello.GetMethod("SayHello");
mi.Invoke(obj, null);
}
// Invoke method.
mi = hello.GetMethod("GetMsg");
Console.WriteLine(mi.Invoke(obj, null));
In effect, you have just created a .NET assembly that is able to create and execute .NET assemblies at
runtime! That wraps up the examination of CIL and the role of dynamic assemblies. I hope this chapter
has deepened your understanding of the .NET type system and the syntax and semantics of CIL.
Source Code The DynamicAsmBuilder project is included under the Chapter 18 subdirectory.
Summary
This chapter provided an overview of the syntax and semantics of CIL. Unlike higher-level managed
languages such as C#