Dim*_*nyi 11 c# plugins appdomain .net-assembly
我正在尝试使用插件进行应用程序.
我有MainLib.dll,我ICommon用1方法制作了一些commnon接口(让它成为).然后,我制作了2个.dll(插件),它们引用了MainLib.dll并ICommon在某些类中实现了.另外,我删除了这个.dll exept中的所有引用System.
然后,我创建了一个应用程序,它监视文件夹".\\Plugins"并加载所有.dll newDomain,检查.dll中的类型是否实现ICommon(因此该应用程序也引用MainLib.dll).如果是 - 在某些列表中添加.dll的名称.
现在问题出现了:在我尝试加载插件之前 - 我将MailLib.dll和System加载到newDomain,因为所有插件都依赖于此.dll.他们加载正确.然后,我开始加载插件,在这里我有:
FileNotFoundException,无法加载文件或程序集'PluginWithException,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'或其依赖项之一.系统找不到指定的文件.)字符串Assembly loadedAssembly = domain.Load(Assembly.LoadFrom(asm).FullName);
PluginWithException程序集只有2个依赖项--System和MainLib.在我尝试加载PluginWithException之前,我检查了新域中的程序集,System和MainLib已加载到此域.所以我看不到任何依赖的问题.我读了这个主题,尝试了解决方案,ProxyDomain但异常是一样的.
我做错了什么?
这里的代码:
public static List<string> SearchPlugins(string[] names)
{
AppDomain domain = AppDomain.CreateDomain("tmpDomain");
domain.Load(Assembly.LoadFrom(@".\MainLib.dll").FullName);
domain.Load(@"System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
MessageBox.Show(GetAssembies(domain)); // here I can see that System and MailLib exist in new domain
List<string> plugins = new List<string>();
foreach (string asm in names)
{
Assembly loadedAssembly = domain.Load(Assembly.LoadFrom(asm).FullName); // here I have exception
var theClassTypes = from t in loadedAssembly.GetTypes()
where t.IsClass &&
(t.GetInterface("ICommonInterface") != null)
select t;
if (theClassTypes.Count() > 0)
{
plugins.Add(asm);
}
}
AppDomain.Unload(domain);
return plugins;
}
Run Code Online (Sandbox Code Playgroud)
您没有指定如何为AppDomains设置搜索路径,以便它可以在Plugins目录中找到DLL,但是您的问题听起来可能与我昨天回答的问题非常类似:
AppDomain.Load()因FileNotFoundException而失败
也许这也会解决你的问题?让我知道你是怎么办的.
您可能想告诉域从哪里加载程序集:
AppDomain domain = AppDomain.CreateDomain("tmpDomain", null, new AppDomainSetup { ApplicationBase = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins") });
Run Code Online (Sandbox Code Playgroud)
但是,我不明白为什么您要在当前(默认)域以及 tmpDomain 中加载程序集。
| 归档时间: |
|
| 查看次数: |
12887 次 |
| 最近记录: |