62,046
社区成员
发帖
与我相关
我的任务
分享
void Application_Start(object sender, EventArgs e)
{
//在应用程序启动时运行的代码
Framework.PlugsHostService.Instance.LoadPlugs();
}
-------------
private bool alreadyLoad = false;
//查找所有的插件的路径
private static List<string> FindPlugin()
{
List<string> PluginPaths = new List<string>();
try
{
//获取当前程序的启动路径
string path = appLocation;
//合并路径,指向插件所在的目录
path = Path.Combine(path, "Lib");
string[] fileNames = Directory.GetFiles(path, "*.dll");
foreach (string fileName in fileNames)
{
PluginPaths.Add(fileName);
}
}
catch (Exception exp)
{
throw exp;
}
return PluginPaths;
}
public void LoadPlugs()
{
if (alreadyLoad)
{
return;
}
List<string> PluginPaths = FindPlugin();
PluginPaths = DeleteInvalidPlugin(PluginPaths);
foreach (string path in PluginPaths)
{
try
{
//获得 文件名称
string asmFile = path;
string asmName = System.IO.Path.GetFileNameWithoutExtension(asmFile);
if (asmFile != string.Empty)
{
// 利用反射,构造DLL文件的实例
System.Reflection.Assembly asm = System.Reflection.Assembly.LoadFile(asmFile);
Type[] t = asm.GetExportedTypes();
foreach (Type type in t)
{
if (type.GetInterface(typeof(IPlugs).FullName) != null)
{
IPlugs iPlugs = (IPlugs)System.Activator.CreateInstance(type);
// 在主程序中使用这个类的实例
//AppDomain.CurrentDomain.Load(asm.GetName());
//AppDomain.CurrentDomain.ExecuteAssembly(asmFile);//.ExecuteAssembly(asmFile);
AppDomain.CurrentDomain.Load(asm.FullName, AppDomain.CurrentDomain.Evidence); //调试时,到这里就报错
}
}
}
alreadyLoad = true;
}
catch (Exception ex)
{
throw ex;
}
}
}
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<!--<probing privatePath="bin;bin2\subbin;bin3"/>-->
<dependentAssembly>
<assemblyIdentity name="MyCheckBoxCtrl" />
<codeBase href="ExtDlls/MyCheckBoxCtrl.dll" />
</dependentAssembly>
</assemblyBinding>
</runtime>
我是要在Application_Start的时候先加载d:\\Lib\\application.dll这个dll,然后再将这个dll注册到当前应用程序域里,所以我用AppDomain.CurrentDomain.Load将其加载;然后我在访问指定页面的时候,就会用到application.dll这个;不是要通过反射立马用dll里的方法
Assembly.LoadFile("D:\\Lib\\application.dll")
AppDomain.CurrentDomain.Load("D:\\Lib\\Application.dll", AppDomain.CurrentDomain.Evidence);