2016-07-13 6 views
0

OpenFileでDLLをロードすると、プラグインのコレクション全体がアセンブリにロードされています。 IPluginインターフェイスで設定された文字列名を使用して、単一のプラグインを読み込むだけで済みます。コレクション全体ではなく単一のジェネリック型を取得する方法

ロードする特定のプラグインのみをロードする方法を教えてください。

public static class GenericPluginLoader<T> 
{ 
    public static ICollection<T> LoadPlugins(string path) 
    { 
     string[] dllFileNames = null; 

     if(Directory.Exists(path)) 
     { 
      dllFileNames = Directory.GetFiles(path, "*.dll"); 

      ICollection<Assembly> assemblies = new List<Assembly>(dllFileNames.Length); 
      foreach(string dllFile in dllFileNames) 
      { 
       AssemblyName an = AssemblyName.GetAssemblyName(dllFile); 
       Assembly assembly = Assembly.Load(an); 
       assemblies.Add(assembly); 
      } 

      Type pluginType = typeof(T); 

      // Type pluginType = .GetType(); 
      ICollection<Type> pluginTypes = new List<Type>(); 
      foreach(Assembly assembly in assemblies) 
      { 
       if(assembly != null) 
       { 
        Type[] types = assembly.GetTypes(); 

        foreach(Type type in types) 
        { 
         if(type.IsInterface || type.IsAbstract) 
         { 
          continue; 
         } 
         else 
         { 
          if(type.GetInterface(pluginType.FullName) != null) 
          { 
           pluginTypes.Add(type); 
          } 
         } 
        } 
       } 
      } 

      ICollection<T> plugins = new List<T>(pluginTypes.Count); 
      foreach(Type type in pluginTypes) 
      { 
       T plugin = (T)Activator.CreateInstance(type); 
       plugins.Add(plugin); 
      } 

      return plugins; 
     } 

     return null; 
    } 
} 

答えて

0

すべてのプラグインが現在のappdomainにロードされないようにする方法を尋ねていると思いますか?その場合は、Assembly.ReflectionOnlyLoadFromを使用して、探しているプラ​​グインを見つけ、個別のプラグインでAssembly.Loadだけを呼び出します。

関連する問題