Skip to content

Instantly share code, notes, and snippets.

@heytherewill
Last active June 20, 2018 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heytherewill/d7893532c4ec200cc387a651cbb50e6a to your computer and use it in GitHub Desktop.
Save heytherewill/d7893532c4ec200cc387a651cbb50e6a to your computer and use it in GitHub Desktop.
Basic IL modification
public class EntryPoint
{
public static void Main(string[] args)
{
// 1
var assemblyLocation = Assembly.GetExecutingAssembly().Location;
var pathToOriginalDll =
Path.GetFullPath(Path.Combine(assemblyLocation, "../path/to/your.dll"));
// 2
var pathToNewDll = pathToOriginalDll.Replace(".dll", "AfterWeaver.dll");
File.Copy(pathToOriginalDll, pathToNewDll, true);
// 3
var moduleDefinition = ModuleDefinition.ReadModule(pathToNewDll);
// 4
var mainMethod = moduleDefinition
.GetTypes()
.Single(t => t.Name.Contains("WhatIWantToWrite"))
.Methods
.Single(m => m.Name.Contains("Main"));
// 5
var processor = mainMethod.Body.GetILProcessor();
var consoleWriteLineInstruction = processor.Body.Instructions.Skip(1).First();
// 6
processor.Append(processor.Create(OpCodes.Ldstr, "This I Wove!"));
processor.Append(consoleWriteLineInstruction);
// 7
var stream = new FileStream(pathToNewDll, FileMode.OpenOrCreate);
moduleDefinition.Write(stream);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment