Last active
June 20, 2018 01:42
-
-
Save heytherewill/d7893532c4ec200cc387a651cbb50e6a to your computer and use it in GitHub Desktop.
Basic IL modification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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