C#如何实现从内存中加载程序集

来源:互联网 发布:行知教育基地 编辑:程序博客网 时间:2024/06/06 11:47

首先,为了动态的在内存中装载程序或程序集,我们以文件流的方式读取二进制文件,并将其以字节的形式保存在数组中,代码如下:

  1. //动态加载插件 
  2.             String pluginFilePath = Path.GetDirectoryName(Application.ExecutablePath) + 
  3.                 "\\plugins\\PluginLibrary.dll"
  4.             FileStream fs = new FileStream(pluginFilePath, FileMode.Open); 
  5.             BinaryReader br = new BinaryReader(fs); 
  6.             byte bin = br.ReadBytes(Convert.ToInt32(fs.Length)); 
  7.             fs.Close(); 
  8.             br.Close(); 

然后,利用 Assembly 类的 Load 重载方法,以数组的形式加载该程序集。代码如下:

  1. Assembly assembly = Assembly.Load(bin); 
0 0
原创粉丝点击