Using AppDomain in C # to dynamically load and unload dlls

In one of my applications, which is related to system diagnostics, the associated DLL must be loaded and unloaded dynamically in C #. After some searching, I found that a single DLL could not be dynamically loaded into the full AppDomain. Therefore, I need to create an AppDomain and use dynamic loading of the DLL dynamically. But I could not find anywhere how I can use this in code. I canโ€™t show the application code because it is against the rules of the company.

Can someone tell me some kind of application code to use it. I want to dynamically load and unload a dll using appdomain and call a specific method in this dll, the dll has no entry point.

Thanks for answers. Ashutosh

+6
source share
4 answers

Thanks guys, here is the link where I found the answer to my question:

Description of the MSDN forum for loading and unloading assemblies dynamically

Another dll can be dynamically loaded and unloaded using another class that loads assemblies and calling methods in this assembly ... AppDomain.CreateInstanceAndUnwrap usually wants input as assemblies from the current project or the current namespace in general. decide that I need Assembly.LoadFrom (); for use in any other class and create an AppDomain and instantiate this class using the AppDomain object, as indicated in the link.

Thanks ur answer guys.

0
source

How to load assemblies in the application domain

public static void Main() { // Use the file name to load the assembly into the current // application domain. Assembly a = Assembly.Load("example"); // Get the type to use. Type myType = a.GetType("Example"); // Get the method to call. MethodInfo myMethod = myType.GetMethod("MethodA"); // Create an instance. object obj = Activator.CreateInstance(myType); // Execute the method. myMethod.Invoke(obj, null); } 

As for how to unload it, you need to unload AppDomain itself, see this

 AppDomain Temporary = AppDomain.CreateDomain("Temporary"); try { Gateway Proxy = (Gateway) Temporary.CreateInstanceAndUnwrap("Shim", "Shim.Gateway"); Match M = Proxy.LoadAndMatch("Plugin.dll", "Though the tough cough and hiccough, plough them through"); } finally { AppDomain.Unload(Temporary); } 
+13
source

It's hard to understand your question, but I will try to make some suggestions.

There is no reason why you cannot dynamically load a dll directly into your application without a separate application domain, the trick is that you cannot upload it. This is important if you can load multiple versions of the same DLL (i.e., you want to update this diagnostic component to a new version without stopping your application). If this is what you are trying to do, I suggest this CodeProject article .

+1
source

In fact, you can dynamically load assemblies into your application domain and run code from it, the problem is that you cannot unload the assembly. However, you can load additional application domains (and assemblies in them) and unload the application domain when you are done.

As its name implies, you have a new application domain, and you canโ€™t just simply call its code and use its types necessary for marching your calls and data across the domain boundaries. If you do a search, you will find many examples of how to do this.

Something to keep in mind is that this is a common template, and there are ready-made solutions for it, the infrastructure itself has a whole addin namespace designed for this type of plug-in behavior, maybe it's worth it at the same time carefully having studied this. There is an article here that shows how to use it.

+1
source

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


All Articles