RAIL

RAIL is the Runtime Assembly Instrumentation Library, which is a project from the University of Coimbra, and which implements “an API that allows CLR assemblies to be manipulated and instrumented before they are loaded and executed.”

One of the examples they give is to add a prologues and epilogues to method calls. I suppose this would be useful for profiling or logging certain method calls. There's more to it than that for sure. This looks interesting.

//Create the RAssemblyDef instance for the Teste3.exe assembly
RAssemblyDef rAssembly = RAssemblyDef.LoadAssembly("Teste3.exe");
//Get the RType object of the FooBar type
RType rtd = rAssembly.RModuleDef.GetType("FooBar");
//Create an array of RParameter objects
RParameter [] paramsz = new RParameter[1];
//Set the first element of the array
paramsz[0] = new RParameter(0,rAssembly.GetType("System.String"));
//Get the method which code is to be inserted at the start of the method
RMethodDef rmd0 = (RMethodDef)rtd.GetMethod("WriteToScreenBefore",rAssembly.GetType("System.Void"),paramsz);
//Get the method which code is to be inserted at the end of the method
RMethodDef rmd1 = (RMethodDef)rtd.GetMethod("WriteToScreenAfter",rAssembly.GetType("System.Void"),paramsz);
//Get the method to instrument
RMethodDef rmd2 = (RMethodDef)rtd.GetMethod("MyMethod",rAssembly.GetType("System.Void"),paramsz);
//Create the CodeTransformer object nCodeTransformer
cc = new CodeTransformer();
//Set the transformations to make in the code
cc.InsertAtMethodStart(rmd0);
cc.InsertAtMethodEnd(rmd1);
//Apply the change to the code
rmd2.Accept(cc);
//Save the new assembly
rAssembly.SaveAssembly("Teste3.exe");

 

1 Comment

Comments have been disabled for this content.