public static IEnumerable <AppDomain> EnumAppDomains()
        {
            IntPtr          enumHandle = IntPtr.Zero;
            ICorRuntimeHost host       = null;

            try
            {
                host = GetCorRuntimeHost();
                host.EnumDomains(out enumHandle);
                object domain = null;

                host.NextDomain(enumHandle, out domain);
                while (domain != null)
                {
                    yield return((AppDomain)domain);

                    host.NextDomain(enumHandle, out domain);
                }
            }
            finally
            {
                if (host != null)
                {
                    if (enumHandle != IntPtr.Zero)
                    {
                        host.CloseEnum(enumHandle);
                    }

                    Marshal.ReleaseComObject(host);
                }
            }
        }
示例#2
0
    public static List <AppDomain> GetAllAppDomains()
    {
        List <AppDomain> appDomains = new List <AppDomain>();

        IntPtr          handle = IntPtr.Zero;
        ICorRuntimeHost host   = (ICorRuntimeHost)(new CorRuntimeHost());

        try
        {
            host.EnumDomains(out handle);
            while (true)
            {
                object domain;
                host.NextDomain(handle, out domain);
                if (domain == null)
                {
                    break;
                }
                appDomains.Add((AppDomain)domain);
            }
        }
        finally
        {
            host.CloseEnum(handle);
        }

        return(appDomains);
    }
示例#3
0
        public static System.AppDomain GetAppDomain(string friendlyName)
        {
            const string method = "GetAppDomain";

            if (friendlyName == null)
            {
                throw new NullParameterException(typeof(ClrUtil), method, "friendlyName");
            }

            System.AppDomain appDomain = null;

            // Enumerate all AppDomains inside the process using the COM hosting interface.

            ICorRuntimeHost corHost = (ICorRuntimeHost) new CorRuntimeHost();

            System.IntPtr hEnum;
            corHost.EnumDomains(out hEnum);

            while (true)
            {
                object nextDomain;
                corHost.NextDomain(hEnum, out nextDomain);
                if (nextDomain == null)
                {
                    break;
                }

                System.AppDomain domain = (System.AppDomain)nextDomain;
                if (domain.FriendlyName == friendlyName)
                {
                    appDomain = domain;
                    break;
                }
            }

            corHost.CloseEnum(hEnum);
            Marshal.ReleaseComObject(corHost);

            return(appDomain);
        }