我正在使用我的MCTS,並正在研究AppDomain功能。但我遇到了一些不清楚的事情。 AppDomain
應該捕獲Exception
並允許域安全地卸載。 (隨着StackOverflowException
的可能除了其他地方建議)AppDomain引起的異常不應該引起異常?
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
Evidence adevidence = AppDomain.CurrentDomain.Evidence;
domain.UnhandledException += new UnhandledExceptionEventHandler(domain_UnhandledException);
AppDomain domain = AppDomain.CreateDomain("MyDomain", adevidence, setup);
當我決定在我爲這個目的,我應該得到一個安全創建的組件創建示例類的實例,限制域名將捕捉髮生並可以安全卸載的錯誤。至少這是我從我的學習書中瞭解到的。
var type = (IDoSomeWork) domain.CreateInstanceAndUnwrap("Library1", "Library1.Class1");
type.Run();
然而,這引發了一個例外type.Run()
(因爲我這樣做)。但不應該安全地捕獲它嗎?這不就是爲什麼我們有AppDomain
?
UPDATE:
按照要求,我已經包括了Library1.Class1
的定義。此外,爲了清楚起見,UnhandledExceptionEventHandler
對捕獲異常沒有影響,並且與問題無關。
[Serializable]
public class Class1 : MarshalByRefObject, IDoSomeWork
{
public void Run()
{
Debug.WriteLine(AppDomain.CurrentDomain.FriendlyName);
throw new ApplicationException(String.Format("{0}", this.ToString()));
}
}
我已經驗證了它在MyDomain
運行。
請給出Library1.Class1的定義。 – leppie 2009-11-10 19:25:01