Loading Referenced DLLs from Memory-Collection of common programming errors

Hey guys,  

I have an application (let’s call it LoaderApp) that reads a byte[] into memory and loads it as an assembly (provided that the byte[] is an executable. This works fine. But how can I set the LoaderApp to also load the referenced DLLs of the byte[] into its Assembly as well?

  The code so far is as follows:  

byte[] bytes = ; Assembly assembly = Assembly.Load(bytes);
MethodInfo method = assembly.EntryPoint;
 
if (method != null)
{ object o = assembly.CreateInstance(method.Name); try { method.Invoke(o, null); } catch (TargetInvocationException e)
    {
            }
}

 
I have read that the loaded assembly can search for its referenced DLLS by itself, as described here: Load Assembly at Runtime Method #3.  

But since the LoaderApp will be loading more than one possible executable, I’d rather have that logic placed in it, rather than for every executable…

 

Thanks a lot

  1. Not the best way to do it, but since no one else provided any suggestions, here’s what I did:  

    1. The LoaderApp loads all the referenced assemblies in memory (as shown in the code snippet of the original question

    2. The loaded Assemblies are passed to a private method implemented in the dynamically loaded assemblies which simply accepts (and stores) an Assembly[] collection. 3. I’ve added to the dynamically loaded assmeblies:

    AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);

    4. For each unresolved assembly, the dynamic assembly checks its Assembly[] collection and returns the matching one as the correct referenced assembly.

  2. Hi,   To get referenced assemblies frorm a specific type, you can try using the code below.
    (typeof(Item).Assembly).GetReferencedAssemblies()

     

    Thank you,