15.01.2013 Views

Free-ebooks-library - Bahar Ali Khan

Free-ebooks-library - Bahar Ali Khan

Free-ebooks-library - Bahar Ali Khan

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

have replaced the call to EmitWriteLine with this, and we would have gotten the<br />

same result:<br />

MethodInfo writeLineStr = typeof (Console).GetMethod ("WriteLine",<br />

new Type[] { typeof (string) });<br />

gen.Emit (OpCodes.Ldstr, "Hello world"); // Load a string<br />

gen.Emit (OpCodes.Call, writeLineStr); // Call a method<br />

Note that we passed typeof(Test) into DynamicMethod’s constructor. This gives the<br />

dynamic method access to the nonpublic methods of that type, allowing us to do<br />

this:<br />

public class Test<br />

{<br />

static void Main()<br />

{<br />

var dynMeth = new DynamicMethod ("Foo", null, null, typeof (Test));<br />

ILGenerator gen = dynMeth.GetILGenerator();<br />

MethodInfo privateMethod = typeof(Test).GetMethod ("HelloWorld",<br />

BindingFlags.Static | BindingFlags.NonPublic);<br />

gen.Emit (OpCodes.Call, privateMethod); // Call HelloWorld<br />

gen.Emit (OpCodes.Ret);<br />

dynMeth.Invoke (null, null); // Hello world<br />

}<br />

static void HelloWorld() // private method, yet we can call it<br />

{<br />

Console.WriteLine ("Hello world");<br />

}<br />

}<br />

Understanding IL requires a considerable investment of time. Rather than understand<br />

all the opcodes, it’s much easier to compile a C# program then to examine,<br />

copy, and tweak the IL. An assembly viewing tool such as ildasm or Lutz Roeder’s<br />

Reflector is perfect for the job.<br />

The Evaluation Stack<br />

Central to IL is the concept of the evaluation stack. The evaluation stack is distinct<br />

from the stack used to store local variables and method parameters.<br />

To call a method with arguments, you first push (“load”) the arguments onto the<br />

evaluation stack, and then call the method. The method then pops the arguments<br />

it needs from the evaluation stack. We demonstrated this previously, in calling<br />

Console.WriteLine. Here’s a similar example with an integer:<br />

var dynMeth = new DynamicMethod ("Foo", null, null, typeof(void));<br />

ILGenerator gen = dynMeth.GetILGenerator();<br />

MethodInfo writeLineInt = typeof (Console).GetMethod ("WriteLine",<br />

new Type[] { typeof (int) });<br />

// The Ldc* op-codes load numeric literals of various types and sizes.<br />

708 | Chapter 18: Reflection and Metadata

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!