Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C#
byte[] bytes = ; // Executable bytes are loaded here...

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)
    {
        // TODO: Handle exception
    }
}


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 :)
Posted

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:
C#
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.
 
Share this answer
 
Hi,

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



Thank you,
 
Share this answer
 
Comments
Trapper-Hell 23-Oct-13 6:54am    
Yes I am aware of that function. But how does that help me in loading the DLLs to the same assembly that I am about to load / or in such a way that the loaded assembly can access them?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900