Assembly.Load 3 dlls

时间:2015-01-28 23:37:29

标签: c#

我有3个dll加载到资源。它们作为嵌入式资源切换。 我有这样的代码只加载一个DLL到Assembly。 如何加载所有dll?

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);
        }
    }

1 个答案:

答案 0 :(得分:2)

你正在调用索引3(奇怪的是它没有失败,因为你有3个dll,它们应该放在[0] [1] [2]。也许是因为你有一个除了dll的资源?无论如何你可以只做一个简单的循环。

    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);
        }

应该这样做