Robin
Robin

Reputation: 123

c# Assembly.LoadFrom with dynamic AssemblyResolve

I'm kind of stuck with dynamically loading plugins:

Theory: (Plugin) Dlls in a specific folder:

foreach(string path in Directory.GetFiles(...))
{
    Assembly myPlugin = Assembly.LoadFrom(path);
    foreach(Type type in myPlugin.GetTypes().Where(t => typeof(myPluginBaseClass).isAssignableFrom(t)))
    {
        Activator.CreateInstance(type);
    }
}

So far so good. Now there are issues with additional references in those assemblies, which can be solved by catch(ReflectionTypeLoadException) (to removed all null Types) and AppDomain.CurrentDomain.AssemblyResolve += ... (to manually look for those missing dlls)

Now here is the issue: The referenced missing assemblies are specific for each implemented Plugin, so I need a individual search behavior implemented in each Plugin. My ideas / solutions so far:

I hope you can help!

Thanks in advance! Robin

Upvotes: 0

Views: 406

Answers (1)

Tony THONG
Tony THONG

Reputation: 772

Create domain seems to be adequat solution for plugin pattern.

You don't really need to define AssemblyResolve, you can simply configure the directory where AppDomain can find dependencies.

https://msdn.microsoft.com/fr-fr/library/system.appdomainsetup.applicationbase(v=vs.110).aspx

Upvotes: 2

Related Questions