Пример #1
0
        public static IEnumerable <AppDomain> GetAppDomains()
        {
            IntPtr enumHandle;

            var host = new mscoree.CorRuntimeHost();

            host.EnumDomains(out enumHandle);
            try
            {
                Object domain;
                do
                {
                    host.NextDomain(enumHandle, out domain);
                    if (domain != null)
                    {
                        yield return((AppDomain)domain);
                    }
                }while (domain != null);
            }
            finally
            {
                host.CloseEnum(enumHandle);
                Marshal.ReleaseComObject(host);
            }
        }
        public static List <AppDomain> GetProcessAppDomains()

        {
            List <AppDomain> result = new List <AppDomain>();

            IntPtr enumHandle = IntPtr.Zero;

            mscoree.CorRuntimeHost host = null;

            try

            {
                host = new mscoree.CorRuntimeHost();

                host.EnumDomains(out enumHandle);

                object domain = null;

                host.NextDomain(enumHandle, out domain);

                while (domain != null)

                {
                    result.Add((AppDomain)domain);

                    host.NextDomain(enumHandle, out domain);
                }
            }

            finally

            {
                if (enumHandle != IntPtr.Zero)

                {
                    host.CloseEnum(enumHandle);
                }

                if (host != null)

                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(host);
                }
            }



            return(result);
        }
Пример #3
0
		public static IEnumerable<AppDomain> GetAppDomains()
		{
			IntPtr enumHandle;

			var host = new mscoree.CorRuntimeHost();
			host.EnumDomains(out enumHandle);
			try
			{
				Object domain;
				do
				{
					host.NextDomain(enumHandle, out domain);
					if (domain != null)
						yield return (AppDomain)domain;
				}
				while (domain != null);
			}
			finally
			{
				host.CloseEnum(enumHandle);
				Marshal.ReleaseComObject(host);
			}
		}
Пример #4
0
 /// <summary>
 /// 获取主域
 /// </summary>
 /// <returns></returns>
 public static AppDomain GetDefaultAppDomain()
 {
     IntPtr enumHandle = IntPtr.Zero;
     mscoree.CorRuntimeHost host = new mscoree.CorRuntimeHost();
     try
     {
         host.EnumDomains(out enumHandle);
         object domain = null;
         while (true)
         {
             host.NextDomain(enumHandle, out domain);
             if (domain == null)
             {
                 break;
             }
             AppDomain appDomain = (AppDomain)domain;
             if (appDomain.IsDefaultAppDomain())
             {
                 return appDomain;
             }
         }
     }
     finally
     {
         host.CloseEnum(enumHandle);
         Marshal.ReleaseComObject(host);
         host = null;
     }
     return null;
 }