Trying to make an expandable application. I am looking for dll files, trying to find classes using this method that implement the ITranslator interface. In another project, everything works. In this - no. I can not understand the reason.

 public List<ITranslator> LoadPlugins() { List<ITranslator> pluginList = new List<ITranslator>(); ITranslator plugin = null; Console.WriteLine(typeof(ITranslator).Module.FullyQualifiedName); try { foreach (var file in Directory.EnumerateFiles(PLUGINS_DIRECTORY, "*.dll", SearchOption.AllDirectories)) { Assembly asm = Assembly.LoadFrom(file); foreach (Type t in asm.GetExportedTypes()) { Console.WriteLine(t.Module.FullyQualifiedName); if (typeof(ITranslator).IsAssignableFrom(t)) { plugin = (ITranslator)asm.CreateInstance(t.FullName); pluginList.Add(plugin); } } } return pluginList; } catch (Exception ex) { Console.WriteLine(ex); Console.WriteLine(string.Format("ΠŸΡ€ΠΎΠ±Π»Π΅ΠΌΠ° ΠΏΡ€ΠΈ сканировании Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΠΈ с ΠΏΠ»Π°Π³ΠΈΠ½Π°ΠΌΠΈ."), "Ошибка ΠΏΡ€ΠΈ Π·Π°Π³Ρ€ΡƒΠ·ΠΊΠ΅ ΠΏΠ»Π°Π³ΠΈΠ½ΠΎΠ²"); return null; } } 

Should add to the list of plugins, but does not add.

 D:\OneDrive\VSProjects\Translator\Translator\bin\Debug\ITranslator.dll D:\OneDrive\VSProjects\Translator\Translator\bin\Debug\Plugins\ITranslator.dll D:\OneDrive\VSProjects\Translator\Translator\bin\Debug\Plugins\YandexTranslator.dll 
  • GetExportedTypes finds your types? They should be public, if that. - Monk
  • Make sure that the class in which the LoadPlugins() method is located and the classes with plug-ins use the same version of the assembly containing ITranslator . If your plug-ins and ITranslator are located within the same Solution before checking the correctness of the LoadPlugins() method, LoadPlugins() recommend doing Rebuild Solution . - sp7
  • @Monk yes, it finds. IsAssignableFrom method does not work ... - mrblamber
  • @sp7 seems to use the same version. There is ITranslator which is obtained from the main project, and there is ITranslator, which is obtained from the YandexTranslator assembly. But in my other project the same, and everything is in order. - Mrblamber
  • one
    @ sp7 just link to it from two assemblies - mrblamber

2 answers 2

The problem is that bin\Debug\ITranslator.dll and bin\Debug\Plugins\ITranslator.dll are two different libraries that contain different types of ITranslator .

Remove the ITranslator.dll library from the Plugins folder.

    If you use MEF , then loading plugins will be much easier: Class with a plugin should be marked:

      using System.ComponentModel.Composition; [Export(typeof(IPlugin))] 

    And then load where you need:

     private static IEnumerable<IPlugin> GetPluginsFrom(string path) { if (Directory.Exists(path)) { try { var container = new CompositionContainer(new DirectoryCatalog(path)); return container.GetExportedValues<IPlugin>(); } catch (System.Exception ex) { Log.Exception(ex, string.Format("Plugins from {0} cannot be loaded.", path)); } } return Enumerable.Empty<IPlugin>(); } 

    Plugins are loaded, as in your case - in the main domain of the application, upload will not work anymore.