Skip to content

Instantly share code, notes, and snippets.

@JoshVarty
Created May 3, 2016 18:19
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 JoshVarty/0b7c0c0ffe6aa51140d6fe62384d8250 to your computer and use it in GitHub Desktop.
Save JoshVarty/0b7c0c0ffe6aa51140d6fe62384d8250 to your computer and use it in GitHub Desktop.
//See part two for how to generate these two
byte[] metadataBytes = ...;
byte[] ilBytes = ...;
//Find module by name
var appDomain = corProcess.AppDomains.Cast<CorAppDomain>().Single();
var assembly = appDomain.Assemblies.Cast<CorAssembly>().Where(n => n.Name.Contains("MyProgram")).Single();
var module = assembly.Modules.Cast<CorModule>().Single();
//I found a bug in the ICorDebug API. Apparently the API assumes that you couldn't possibly have a change to apply
//unless you had first fetched the metadata for this module. Perhaps reasonable in the overall scenario, but
//its certainly not OK to simply throw an AV exception if it hadn't happened yet.
//
//In any case, fetching the metadata is a thankfully simple workaround
object import = module.GetMetaDataInterface(typeof(IMetadataImport).GUID);
corProcess.Stop(-1);
module.ApplyChanges(metadataBytes, ilBytes);
corProcess.Continue(outOfBand: false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment