Load multiple assemblies

不羁的心 提交于 2020-01-07 03:51:13

问题


How can I load an assembly using its full display name from a location outside the application's bin path?

Usually one can load an assembly from a custom location with

Assembly.LoadFrom(path);

This works, but it seems that for loading a strong-named assembly I need to specify its full display name such as in

Assembly myDll =
Assembly.Load("myDll, Version=1.0.0.1, Culture=neutral, PublicKeyToken=9b35aa32c18d4fb1");

But the problem here is that this only references assemblies that are in my probing path of my application.

So what if I have an assembly dir1/asm.dll and one assembly dir2/asm.dll and both have a strong name.

How can I load them during runtime?


回答1:


During Runtime, you can specify additional directories to probe when loading an assembly via the following methods:

    AppDomain.CurrentDomain.ClearPrivatePath();
    AppDomain.CurrentDomain.AppendPrivatePath();

When the subdirectory names are already known during installation, you can also specify these additional directories in the app.config file in the privatePath attribute of the <probing> element.

Make also sure the file name is correct. When you have

AppDomain.CurrentDomain.AppendPrivatePath("Subdir");

Assembly myDll = Assembly.Load("myDll, Version=1.0.0.1, Culture=neutral, PublicKeyToken=9b35aa32c18d4fb1");

then .net will look for a file named "mydll.dll" in the directory "Subdir" beneath the directory of the executable.




回答2:


you can load any assembly from its location via loadfile (example here)

if you want to take multiple versions of an assembly consider handling the AppDomain.CurrentDomain.AssemblyResolve (example here)

the examples are from a small open source project, which will load dlls from a seperate a "packages" folder (allowing for packages to have their own copy of a dependency, using the isolated loader)



来源:https://stackoverflow.com/questions/35109200/load-multiple-assemblies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!