开发者

C#: loop through appdomains in assembly [closed]

It's difficult to tell what is being asked here. This question is ambi开发者_C百科guous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

how to loop through appdomains in assembly?


Source: Thomas Scheidegger

You need to add the following as a COM reference - ~\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscor ee.tlb. This works with currnet executing assembly. If you want to loop though processes and then get appdomain, that may not be possbile.

 public void GetAllAppDomains()
             {

                 AppDomain one = AppDomain.CreateDomain("One");
                 AppDomain two = AppDomain.CreateDomain("Two"); 
                // Creates 2 app domains           

                 List<AppDomain> appDomains = new List<AppDomain>();

                 IntPtr enumHandle = IntPtr.Zero;

                 CorRuntimeHostClass host = new CorRuntimeHostClass();          

                 try
                 {

                     host.EnumDomains(out enumHandle);

                     object domain = null;

                     AppDomain tempDomain;

                     while (true)
                     {

                         host.NextDomain(enumHandle, out domain);

                         if (domain == null)
                         {
                             break;
                         }

                         tempDomain = domain as AppDomain;

                         appDomains.Add(tempDomain);

                     }               

                 }

                 catch (Exception ex)
                 {
                     Console.WriteLine(ex.ToString());          
                 }

                 finally
                 {
                     host.CloseEnum(enumHandle);
                     int rel= Marshal.ReleaseComObject(host);
                 }

                 Assembly[] assemblies;
                 foreach (AppDomain app in appDomains)
                 {
                     Console.WriteLine(app.FriendlyName);

                     assemblies = app.GetAssemblies();

                     Console.WriteLine("-----------------------Assemblies------------------");
                     foreach (Assembly assem in assemblies)
                     {
                         Console.WriteLine(assem.FullName);
                     }
                     Console.WriteLine("---------------------------------------------------");
                 }

             }


Assuming you mean "in the process"... an AppDomain is such a significant item that you should really know when you are creating one, and track/manage the lifetime. There isn't an easy way to find all the AppDomains in the process, AFAIK.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜