在运行时从DLL加载类型

本文关键字:加载 类型 DLL 运行时 | 更新日期: 2023-09-27 18:05:37

我需要在运行时加载的程序集中执行一个方法。我要加载的程序集是包含接口实现的插件。

这是加载类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
namespace Stub.Logic {
    public class DllReader {
        private static List<Type> connectionTypes = new List<Type>();
        public static void LoadConnectionTypes(string path) {
            DirectoryInfo dllDirectory = new DirectoryInfo(path);
            FileInfo[] dlls = dllDirectory.GetFiles("*.dll");
            foreach (FileInfo dllFileInfo in dlls) {
                Assembly assembly = Assembly.Load(dllFileInfo.FullName);
                connectionTypes.AddRange(assembly.GetTypes());
            }
        }
        //public static Connection GetConnection(string connectionTypeName) {
        //    return new Connection();
        //}
    }
}

我得到这个错误:

无法加载文件或程序集"..'Plugins'MqConnection.dll"或一个它的依赖关系。给定的程序集名称或代码库无效。(Exception from HRESULT: 0x80131047)

这是加载的程序集:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;
using Stub.Logic;
namespace MqConnection {
    public class MqConnection : Connection {
        // Stuff here...
    }
}

我做错了什么?

在运行时从DLL加载类型

程序集。加载接受程序集名称,而不是文件路径。使用Assebmly。LoadFrom而不是