首先,为了动态的在内存中装载程序或程序集,我们以文件流的方式读取二进制文件,并将其以字节的形式保存在数组中,代码如下:
- //动态加载插件
- String pluginFilePath = Path.GetDirectoryName(Application.ExecutablePath) +
- "\\plugins\\PluginLibrary.dll";
- FileStream fs = new FileStream(pluginFilePath, FileMode.Open);
- BinaryReader br = new BinaryReader(fs);
- byte[] bin = br.ReadBytes(Convert.ToInt32(fs.Length));
- fs.Close();
- br.Close();
然后,利用 Assembly 类的 Load 重载方法,以数组的形式加载该程序集。代码如下:
- Assembly assembly = Assembly.Load(bin);