Load static class in appdomain

There is a big problem in C # AppDomain.

I need to load a static class into a DLL file and execute its method:

  • When I try to download them using

    Assembly.LoadFrom("XXXXX") // (XXXXX is the full path of dll) 

    .dll will not be downloaded automatically or programmatically.

  • When I try to load them into AppDomain, for example

     adapterDomain = AppDomain.CreateDomain("AdapterDomain"); (a)adapterDomain.CreateInstanceFrom(this.AdapterFilePath, this.AdapterFullName); (b)adapterAssembly=adapterDomain.Load(AssemblyName.GetAssemblyName(this.AdapterFilePath)); 

    If I use method (a) because the target class is static, it does not work.

    If I use method (b) because the target .dll is not the same directory with my project, I will get an exception.

How to load a DLL file and a static class, and then unload the DLL after my use?

+4
source share
1 answer

Method (b) fails because AppDomain.Load cannot resolve assemblies that are not in the base application directory, private probing paths, or the GAC.

Also note that AppDomain.Load does not load the assembly on a specific AppDomain (e.g. adapterDomain.Load in your sample code). Instead, it loads it into the current AppDomain (this is the one that makes the AppDomain.Load call. This behavior is noted in

To complete this work, you will need to replace:

PATH_TO_ASSEMBLY indicating the assembly path containing the static method, including the extension. CLASS_CONTAINING_STATIC_METHOD with the name of the class containing the static method, including the class namespace. STATIC_METHOD with the name of the static method.

Note that BindingFlags set for public static methods.

+5
source

Source: https://habr.com/ru/post/1501759/


All Articles