对于mscoree.dll程序集的引用请使用com引用:c:windowsmicrosoft.netframeworkvxxxxxxmscoree.tlb
public static ilist
getappdomains()
{
ilistlist = new list ();
intptr enumhandle = intptr.zero;
corruntimehostclass host = new corruntimehostclass();
try
{
host.enumdomains(out enumhandle);
object domain = null;
while (true)
{
host.nextdomain(enumhandle, out domain);
if (domain == null) break;
appdomain appdomain = (appdomain)domain;
list.add(appdomain);
}
return list;
}
catch (exception e)
{
return null;
}
finally
{
host.closeenum(enumhandle);
marshal.releasecomobject(host);
}
}
.net framework居然没有提供托管的接口来获取当前进程中的其它appdomain!所以,我们只有借助承载接口(hosting interfaces)来完成这事了。
在mscoree.dll中,.net 1.0提供了一个icorruntimehost接口,该接口中有enumdomains和nextdomain方法,而幸运的是:mscoree.dll中居然直接提供了一个public的实现:corruntimehostclass。所以,我们只需要新建一个corruntimehostclass然后调用enumdomains和nextdomain