Free mag vol1 | Page 696

CHAPTER 17  PROCESSES, APPDOMAINS, AND OBJECT CONTEXTS ***** Fun with the default AppDomain ***** Name of this domain: DefaultAppDomainApp.exe ID of domain in this process: 1 Is this the default domain?: True Base directory of this domain: E:\MyCode\DefaultAppDomainApp\bin\Debug\ Notice that the name of the default application domain will be identical to the name of the executable that is contained within it (DefaultAppDomainApp.exe, in this example). Also notice that the base directory value, which will be used to probe for externally required private assemblies, maps to the current location of the deployed executable. Enumerating Loaded Assemblies It is also possible to discover all of the loaded .NET assemblies within a given application domain using the instance-level GetAssemblies() method. This method will return to you an array of Assembly objects, which as you recall from the Chapter 15, is a member of the System.Reflection namespace (so don’t forget to import this namespace into your C# code file!). To illustrate, define a new method named ListAllAssembliesInAppDomain() within the Program class. This helper method will obtain all loaded assemblies, and print out the friendly name and version of each. static void ListAllAssembliesInAppDomain() { // Get access to the AppDomain for the current thread. AppDomain defaultAD = AppDomain.CurrentDomain; } // Now get all loaded assemblies in the default AppDomain. Assembly[] loadedAssemblies = defaultAD.GetAssemblies(); Console.WriteLine("***** Here are the assemblies loaded in {0} *****\n", defaultAD.FriendlyName); foreach(Assembly a in loadedAssemblies) { Console.WriteLine("-> Name: {0}", a.GetName().Name); Console.WriteLine("-> Version: {0}\n", a.GetName().Version); } Assuming you have updated your Main() method to call this new member, you will see that the application domain hosting your executable is currently making use of the following .NET libraries: ***** Here are the assemblies loaded in DefaultAppDomainApp.exe ***** -> Name: mscorlib -> Version: 4.0.0.0 -> Name: DefaultAppDomainApp 639