I try to add an addons system to my Windows.Net application using Reflection; but it fails when there is addon with dependencie.
Addon class have to implement an interface ‘IAddon’ and to have an empty constructor.
Main program load the addon using Reflection:
Assembly assembly = Assembly.LoadFile(@'C:\Temp\TestAddon\Addon.dll'); Type t = assembly.GetType('Test.MyAddon'); ConstructorInfo ctor = t.GetConstructor(new Type[] { }); IAddon addon= (IAddon) ctor.Invoke(new object[] { }); addon.StartAddon();
It works great when addon do not use dependencie. But if my addon reference and use an another DLL (C:\Temp\TestAddon\MyTools.dll) that is saved near the addon in disk, it fails:
System.IO.FileNotFoundException: Could not load file or assembly ‘MyTools.dll’ or one of its dependencies.
I do not wants to copy the addons DLL near my executable, how can i do to tell .Net runtime to search in ‘C:\Temp\TestAddon\’ for any dependency?
Note that adding
Assembly assembly = Assembly.LoadFile(@'C:\Temp\TestAddon\MyTools.dll');
do not change anything.
If MyTools.dll is located in the same directory as Addon.dll, all you need to do is call
Assembly.LoadFrominstead ofAssembly.LoadFileto make your code work. Otherwise, handling theAppDomain.AssemblyResolveevent is the way to go.