Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I just tried to recreate your solution as best as I can, and I have no such issues. (Warning, lots of code samples follow....)</p> <p>First project is the application, this contains one class:</p> <pre><code>public class PluginLoader : ILoader { private List&lt;Type&gt; _providers = new List&lt;Type&gt;(); public PluginLoader() { LoadProviders(); LoadConsumers(); } public IProvider RequestProvider(Type providerType) { foreach(Type t in _providers) { if (t.GetInterfaces().Contains(providerType)) { return (IProvider)Activator.CreateInstance(t); } } return null; } private void LoadProviders() { DirectoryInfo di = new DirectoryInfo(PluginSearchPath); FileInfo[] assemblies = di.GetFiles("*.dll"); foreach (FileInfo assembly in assemblies) { Assembly a = Assembly.LoadFrom(assembly.FullName); foreach (Type type in a.GetTypes()) { if (type.GetInterfaces().Contains(typeof(IProvider))) { _providers.Add(type); } } } } private void LoadConsumers() { DirectoryInfo di = new DirectoryInfo(PluginSearchPath); FileInfo[] assemblies = di.GetFiles("*.dll"); foreach (FileInfo assembly in assemblies) { Assembly a = Assembly.LoadFrom(assembly.FullName); foreach (Type type in a.GetTypes()) { if (type.GetInterfaces().Contains(typeof(IConsumer))) { IConsumer consumer = (IConsumer)Activator.CreateInstance(type); consumer.Initialize(this); } } } } </code></pre> <p>Obviously this can be tidied up enormously. </p> <p>Next project is the shared library which contains the following three interfaces:</p> <pre><code>public interface ILoader { IProvider RequestProvider(Type providerType); } public interface IConsumer { void Initialize(ILoader loader); } public interface IProvider { } </code></pre> <p>Finally there is the plugin project with these classes:</p> <pre><code>public interface ITest : IProvider { } public class TestConsumer : IConsumer { public void Initialize(ILoader loader) { ITest tester = (ITest)loader.RequestProvider(typeof (ITest)); } } public class TestProvider : ITest { } </code></pre> <p>Both the application and the plugin projects reference the shared project and the plugin dll is copied to the search directory for the application - but they don't reference one another.</p> <p>When the PluginLoader is constructed it finds all the IProviders then creates all the IConsumers and calls Initialize on them. Inside the initialize the consumer can request providers from the loader and in the case of this code a TestProvider is constructed and returned. All of this works for me with no fancy control of the loading of assemblies.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload