プラグインを使ってアプリケーションを作ろうとしています。
私は MainLib.dll を持っています。ここでICommon
、1 つのメソッドを使用していくつかの共通インターフェイスを作成しました。次に、MainLib.dll への参照を持ちICommon
、いくつかのクラスに実装する 2 つの .dll(プラグイン) を作成しました。また、この .dlls exept 内のすべての参照を削除しましたSystem
。
次に、フォルダーを監視し、.dll 内の".\\Plugins"
すべての .dll をロードするアプリケーションを作成しnewDomain
、.dll の型が実装されているかどうかを確認しますICommon
(したがって、このアプリケーションは MainLib.dll も参照します)。はいの場合 - リストに .dll の名前を追加します。
そしてここで問題:プラグインをロードしようとする前に、すべてのプラグインがこの .dll に依存しているため、MailLib.dll と System を newDomain にロードします。それらは正しく読み込まれます。次に、プラグインのロードを開始すると、次のようになります。
FileNotFoundException、ファイルまたはアセンブリ 'PluginWithException、Version=1.0.0.0、Culture=neutral、PublicKeyToken=null' またはその依存関係の 1 つを読み込めませんでした。指定されたファイルが見つかりません。) on string Assembly loadedAssembly = domain.Load(Assembly.LoadFrom(asm).FullName);
PluginWithException アセンブリには、System と MainLib の 2 つの依存関係しかありません。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;
}