简体   繁体   中英

Load assembly from a path without copying it to bin folder

I have a simple c# application and I would like to reference an assembly from C:\\..\\..myassembly.dll without copying the thing to bin folder.

I there a way to change how is assembly being referenced/loaded?

Edit: the assembly is not in gac

Actually it is possible during runtime by using <codeBase> tag in your configuration:

<configuration>
<runtime>
   <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
         <assemblyIdentity name="MyAssembly2"  culture="neutral" publicKeyToken="307041694a995978"/>
         <codeBase version="1.0.1524.23149" href="FILE://C:/Myassemblies/MyAssembly2.dll"/>
      </dependentAssembly>
   </assemblyBinding>
</runtime>
</configuration>

You can refer to the MSDN to get more specific info. The downside of this solution is that you have to manually specify assembly version.

Multiple solutions to your problem.

To avoid copying library to output folder set Copy Local to false by selecting .dll property.

Option 1:

Include below section in your configuration file and let allow runtime to load from location you specified.

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="MyAssembly" publicKeyToken="2d65620afb84f19d" />
        <codeBase version="1.0.0.0" href="C:\..\..myassembly.dll" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

Option 2:

Well known option, use reflection to load your assembly.

Assembly.Load(@"C:\..\..myassembly.dll")

Hope this helps !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM