Problem:
In many cases, we want to dynamically loadProgramTo release the loaded DLL file in time to release resources and update DLL files.
Solution:
Currently, CLR does not provide a method to uninstall an assembly. While assembly is resident in appdomain, appdomain can be dynamically detached and created.
For example, there is a dynamically generated Class Library:
Public Class Helloworld: Export albyrefobject
{
Public Helloworld ()
{
}
Public Void Task1 ( String S)
{
Console. writeline ("Task1" +S );
}
}
Dynamic call // File: invoke. CS
Using System;
Using System. reflection;
Using System. runtime. remoting;
Public Class Invokemethod
{
Public Static Void Main (string [] argv)
{
Appdomainsetup info = New Appdomainsetup ();
Info. applicationbase = " File :/// " + System. environment. currentdirectory;
Appdomain dom = Appdomain. createdomain ( " Remotedomain " , Null , Info );
Assembly ASM = Assembly. Load ( " Helloworld2 " );
Object OBJ = ASM. createinstance ( " Helloworld " );
Methodinfo minfo = ASM. GetType ( " Helloworld " ). Getmethod ( " Task1 " );
Minfo. Invoke (OBJ, New String [] {"Task 1"} );
Appdomain. Unload (DOM );
}
}
Restrictions:
The DLL to be dynamically loaded must be in the same directory as the host.
Refer:
Http://www.gotdotnet.com/team/clr/AppdomainFAQ.aspx#_Toc514058481