?? 顯式卸載應用程序域.txt
字號:
顯式卸載應用程序域 AppDomain.Unload
跨越應用程序邊界、并通通引用的方式來進行封送處理的類型。
using System;
using System.Reflection;
using System.Threading;
class MarshalByRefType:MarshalByRefObject
{
public void SomeMethod(String sourceAppDomain)
{
Console.WriteLine("Code from the '{0}' AppDomain\n"+
"called into the '{1}' AppDomain.",sourceAppDomain,Thread.GetDomain().FriendlyName);
}
}
class App
{
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//創建一個新應用程序域
AppDomain ad=AppDomain.CreateDomain("MyNewAppDomain",null,null);
//在新的程序域中創建對象
MarshalByRefType mbrt=(MarshalByRefType)ad.CreateInstanceAndUnwrap(Assembly.GetCallingAssembly().FullName,"MarshalByRefType");
//調用對象的方法
mbrt.SomeMethod(Thread.GetDomain().FriendlyName);
//卸載應有程序域及其內所有程序集
AppDomain.Unload(ad);
try
{
//試圖再調用卸載對象的方法,有異常
mbrt.SomeMethod(Thread.GetDomain().FriendlyName);
Console.WriteLine("Called SomeMethod on object in other AppDomain.\nThis shouldn't happen.");
}
catch(AppDomainUnloadedException)
{
Console.WriteLine("Fail to Called SomeMethod on object in other AppDomain.\nThis should happen.");
}
Console.WriteLine();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -