Browse Source

Allow assembly hot replacement from stream

pull/523/head
Sebastien LEBRETON 11 years ago
parent
commit
560eb73565
  1. 23
      ILSpy/AssemblyList.cs
  2. 22
      ILSpy/LoadedAssembly.cs

23
ILSpy/AssemblyList.cs

@ -146,6 +146,29 @@ namespace ICSharpCode.ILSpy
return newAsm; return newAsm;
} }
/// <summary>
/// Replace the assembly object model from a crafted stream, without disk I/O
/// Returns null if it is not already loaded.
/// </summary>
public LoadedAssembly HotReplaceAssembly(string file, Stream stream)
{
App.Current.Dispatcher.VerifyAccess();
file = Path.GetFullPath(file);
var target = this.assemblies.FirstOrDefault(asm => file.Equals(asm.FileName, StringComparison.OrdinalIgnoreCase));
if (target == null)
return null;
var index = this.assemblies.IndexOf(target);
var newAsm = new LoadedAssembly(this, file, stream);
lock (assemblies)
{
this.assemblies.Remove(target);
this.assemblies.Insert(index, newAsm);
}
return newAsm;
}
public void Unload(LoadedAssembly assembly) public void Unload(LoadedAssembly assembly)
{ {
App.Current.Dispatcher.VerifyAccess(); App.Current.Dispatcher.VerifyAccess();

22
ILSpy/LoadedAssembly.cs

@ -35,7 +35,7 @@ namespace ICSharpCode.ILSpy
readonly string fileName; readonly string fileName;
readonly string shortName; readonly string shortName;
public LoadedAssembly(AssemblyList assemblyList, string fileName) public LoadedAssembly(AssemblyList assemblyList, string fileName, Stream stream = null)
{ {
if (assemblyList == null) if (assemblyList == null)
throw new ArgumentNullException("assemblyList"); throw new ArgumentNullException("assemblyList");
@ -44,7 +44,7 @@ namespace ICSharpCode.ILSpy
this.assemblyList = assemblyList; this.assemblyList = assemblyList;
this.fileName = fileName; this.fileName = fileName;
this.assemblyTask = Task.Factory.StartNew<ModuleDefinition>(LoadAssembly); // requires that this.fileName is set this.assemblyTask = Task.Factory.StartNew<ModuleDefinition>(LoadAssembly, stream); // requires that this.fileName is set
this.shortName = Path.GetFileNameWithoutExtension(fileName); this.shortName = Path.GetFileNameWithoutExtension(fileName);
} }
@ -93,12 +93,26 @@ namespace ICSharpCode.ILSpy
get { return assemblyTask.IsFaulted; } get { return assemblyTask.IsFaulted; }
} }
ModuleDefinition LoadAssembly() ModuleDefinition LoadAssembly(object state)
{ {
var stream = state as Stream;
ModuleDefinition module;
// runs on background thread // runs on background thread
ReaderParameters p = new ReaderParameters(); ReaderParameters p = new ReaderParameters();
p.AssemblyResolver = new MyAssemblyResolver(this); p.AssemblyResolver = new MyAssemblyResolver(this);
ModuleDefinition module = ModuleDefinition.ReadModule(fileName, p);
if (stream != null)
{
// Read the module from a precrafted stream
module = ModuleDefinition.ReadModule(stream, p);
}
else
{
// Read the module from disk (by default)
module = ModuleDefinition.ReadModule(fileName, p);
}
if (DecompilerSettingsPanel.CurrentDecompilerSettings.UseDebugSymbols) { if (DecompilerSettingsPanel.CurrentDecompilerSettings.UseDebugSymbols) {
try { try {
LoadSymbols(module); LoadSymbols(module);

Loading…
Cancel
Save