How to reference a dll outside the application's folder and utilize its methods and interfaces with loading it dynamically? is there a way to do that?
Note: the reference must be made from that path even after the application gets deployed.
How to reference a dll outside the application's folder and utilize its methods and interfaces with loading it dynamically? is there a way to do that?
Note: the reference must be made from that path even after the application gets deployed.
I would try using Assembly.LoadFrom(string)
. This overload takes the path to the assembly and allows you to utilize it via reflection.
You can get the assembly like this:
var sampleAssembly = Assembly.LoadFrom("c:\\Sample.Assembly.dll");
var method = sampleAssembly.GetTypes()[0].GetMethod("Method1");
Then invoke that method using MethodInfo.Invoke()
There are many ways to do that. Detailed explanation of how runtime locates assemblies can be found at How runtime locates assemblies.
You can specify exact location via codeBase
element in app.config
:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="myAssembly"
publicKeyToken="32ab4ba45e0a69a1"
culture="neutral" />
<codeBase version="2.0.0.0"
href="http://www.litwareinc.com/myAssembly.dll"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
If your assembly is in subfolder of application then you can use probing
element:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="bin;bin2\subbin;bin3"/>
</assemblyBinding>
</runtime>
</configuration>
If all that fails, you can always write event handler and attach it to AppDomain.AssemblyResolve to explicitly load assemblies. There is even example on SO.