uizhc
uizhc

Reputation: 33

Assembly.Load 3 dlls

I have 3 dlls loaded to resourses. They are switched as Embedded resources. I have such code which loads only one dll to Assembly. How to load all dlls?

public partial class Main : Form
{
    public Main()
    {
        AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolve;
        InitializeComponent();

    }

    public static Assembly AssemblyResolve(object sender, ResolveEventArgs args)
    {
        Assembly assembly = Assembly.GetExecutingAssembly();
        using (Stream stream = assembly.GetManifestResourceStream(assembly.GetManifestResourceNames()[3]))
        {
            if (stream == null)
                return null;

            byte[] rawAssembly = new byte[stream.Length];
            stream.Read(rawAssembly, 0, (int)stream.Length);  
            return Assembly.Load(rawAssembly);
        }
    }

Upvotes: 1

Views: 90

Answers (1)

Bauss
Bauss

Reputation: 2797

You're calling index 3 (Strangely it doesn't fail as you have 3 dll's they should be placed at [0][1][2]. Perhaps because you have a resource apart from the dlls? Anyways you can just do a simple loop.

    for (int i = 1; i <= 3; i++) // Your dll's seem to be stored from index 1
    using (Stream stream = assembly.GetManifestResourceStream(assembly.GetManifestResourceNames()[i]))
        {
            if (stream == null)
                return null;

            byte[] rawAssembly = new byte[stream.Length];
            stream.Read(rawAssembly, 0, (int)stream.Length);  
            return Assembly.Load(rawAssembly);
        }

That should do it

Upvotes: 2

Related Questions