主AppDomain有一些子AppDomain。在子AppDomain中可能是UnhandledException。 如何在子AppDomain中通过UnhandledException卸载子AppDomain。
答案 0 :(得分:3)
这可以通过try ... finally ...
完成,而无需注册UnhandledException事件处理程序:
using System;
using System.Reflection;
interface IFoo
{
void DoGood();
void DoBad();
}
class Foo : MarshalByRefObject, IFoo
{
public void DoGood() { Console.WriteLine("I'm good (" + AppDomain.CurrentDomain.FriendlyName + ")"); }
public void DoBad() { throw new Exception("I'm bad (" + AppDomain.CurrentDomain.FriendlyName + ")"); }
}
class Program
{
public static void Main()
{
try
{
AppDomain domain = AppDomain.CreateDomain("FooDomain");
try
{
string assemblyName = Assembly.GetExecutingAssembly().FullName; // may be different assembly
string typeName = "Foo";
IFoo foo = (IFoo)domain.CreateInstanceAndUnwrap(assemblyName, typeName);
foo.DoGood();
foo.DoBad();
}
finally
{
AppDomain.Unload(domain);
}
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message);
}
}
}
输出结果为:
I'm good (FooDomain)
Error: I'm bad (FooDomain)