In the end, I did it almost as raboof suggested (and looks like what dgvid suggested), with the exception of some minor changes and some omissions. I chose this method because it was closest to what I was looking for in the first place, and did not require the use of any third-party executables, etc. It works great!
Here is what my code looked like:
EDIT: I decided to move this function to another assembly in order to reuse it in multiple files (I just pass Assembly.GetExecutingAssembly ()).
This is an updated version that allows you to go through an assembly with embedded DLLs.
embeddedResourcePrefix is ββthe string path to the embedded resource, usually the name of the assembly, followed by any folder structure that contains the resource (for example, "MyComapny.MyProduct.MyAssembly.Resources" if the DLL is in the Resources folder in the project). It also suggests that the dll has the .dll.resource extension.
public static void EnableDynamicLoadingForDlls(Assembly assemblyToLoadFrom, string embeddedResourcePrefix) { AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => { // had to add => try { string resName = embeddedResourcePrefix + "." + args.Name.Split(',')[0] + ".dll.resource"; using (Stream input = assemblyToLoadFrom.GetManifestResourceStream(resName)) { return input != null ? Assembly.Load(StreamToBytes(input)) : null; } } catch (Exception ex) { _log.Error("Error dynamically loading dll: " + args.Name, ex); return null; } }; // Had to add colon } private static byte[] StreamToBytes(Stream input) { int capacity = input.CanSeek ? (int)input.Length : 0; using (MemoryStream output = new MemoryStream(capacity)) { int readLength; byte[] buffer = new byte[4096]; do { readLength = input.Read(buffer, 0, buffer.Length); // had to change to buffer.Length output.Write(buffer, 0, readLength); } while (readLength != 0); return output.ToArray(); } }
Lawrence Johnston Sep 19 '08 at 17:11 2008-09-19 17:11
source share