I'm trying to write IL that calls methods in mscorlib, but I can't figure out how to get a ModuleDefinition
to mscorlib to actually reference the types & methods, and documentation & google are lacking.
Getting a ModuleDefinition for the mscorlib is pretty easy. Here's a simple way:
ModuleDefinition corlib = ModuleDefinition.ReadModule (typeof (object).Module.FullyQualifiedName);
But if you inject code that is calling methods in the mscorlib, you don't necessarily have to load the module yourself. For instance:
MethodDefinition method = ...;
ILProcessor il = method.Body.GetILProcessor ();
Instruction call_writeline = il.Create (
OpCodes.Call,
method.Module.Import (typeof (Console).GetMethod ("WriteLine", Type.EmptyTypes)));
Creates an instruction to call Console.WriteLine ();
As for the documentation, please read the importing page on the wiki.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With