C # Dynamic loading method for unloading DLLs

Source: Internet
Author: User

This article describes the C # dynamic load Unload DLL method, the need for friends can refer to

It is easy to load DLL assemblies dynamically with reflection in C #, but if you need to update the DLLs, you find that the. NET class library does not provide a way to unload the DLL assembly. In. NET, the concept of an application domain is added, and the application domain can be uninstalled. That is, if you need to update a dynamically loaded DLL assembly, you can resolve it in the following ways:

Create a new application domain, load the DLL dynamically in the application domain, and then unload the application domain. When the application domain is unloaded, the associated resources are also recycled.

To do this, you need to communicate between the CurrentDomain of your program and the new NewDomain, passing through the boundaries of the application domain. From the Internet to find a solution to a Daniel, copy down for yourself to see it:

usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Threading;usingSystem.Reflection;namespaceUnloaddll {classProgram {Static voidMain (string[] args) {             stringCallingdomainname = AppDomain.CurrentDomain.FriendlyName;//Thread.getdomain (). FriendlyName; Console.WriteLine (Callingdomainname); AppDomain AD= Appdomain.createdomain ("DLL Unload Test"); Proxyobject obj= (proxyobject) ad. CreateInstanceFromAndUnwrap (@"UnloadDll.exe","Unloaddll.proxyobject"); Obj.             LoadAssembly (); Obj. Invoke ("Testdll.class1","Test","It ' s a test");             AppDomain.Unload (AD); Obj=NULL;         Console.ReadLine (); }     }     classproxyobject:marshalbyrefobject {Assembly Assembly=NULL;  Public voidloadassembly () {Assembly= Assembly.loadfile (@"TestDLL.dll"); }          Public BOOLInvoke (stringFullClassName,stringMethodName,paramsobject[] args) {             if(Assembly = =NULL)                 return false; Type TP=Assembly.             GetType (FullClassName); if(TP = =NULL)                 return false; MethodInfo Method=TP.             GetMethod (MethodName); if(Method = =NULL)                 return false; Object obj=activator.createinstance (TP); Method.             Invoke (obj, args); return true; }     } } 

Attention:

1. To allow an object to pass through the AppDomain boundary, you must inherit the MarshalByRefObject class, otherwise it cannot be used by another AppDomain.

2. Each thread has a default AppDomain, which can be obtained by Thread.getdomain ()

Articles you may be interested in:
    • A detailed description of the DLL methods generated by C # calling Matlab
    • C # calls Delphi DLL Instance code
    • C # Call Dynamic Unlha32.dll extract LHA suffix for packaged file sharing
    • C # Invocation of C + + Write DLL implementation method
    • C # Concrete implementation of packaging DLLs into programs
    • C + + calls to C # DLL implementation method
    • Steps for implementing C + + and C # intermodulation DLLs
    • Methods for calling unmanaged DLLs from a C # program
    • C # Implements a method for dynamically loading DLLs
    • C # Methods for dynamically loading DLLs to extend system functionality
    • C # Methods for generating DLL files
    • A summary of how C # generates DLL files

Citation Links:

C # Dynamic loading method for unloading DLLs

C # implements dynamic load DLL plug-ins and hresult:0x80131047 processing

C # implements dynamic load DLLs

Huang Tian not negative, finally full body and retreat, haha

Dynamically loading DLLs, extending system functionality

C # dynamically loads DLLs and calls methods of classes in DLLs

C # Dynamic loading method for unloading DLLs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.