Examples shows
-Display domain stats
-List assemblies in domain
-List assemblies in domain through LINQ
-EventHandling of assembly loading
-Load Unload Assemblies, HandlingEvents
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Reflection; namespace domainExamples { class Program { static void Main(string[] args) { //DisplayDomainStats(); //ListAllAssembliesInAppDomain2(); CreateNewAppDomain(); Console.ReadLine(); } static void DisplayDomainStats() { AppDomain currentDomain = AppDomain.CurrentDomain; Console.WriteLine("Name of this domain: {0}", currentDomain.FriendlyName); Console.WriteLine("ID of this domain: {0}", currentDomain.Id); Console.WriteLine("Is Default Domain ?: {0}", currentDomain.IsDefaultAppDomain()); Console.WriteLine("Base directory of this domain: {0}", currentDomain.BaseDirectory); } static void ListAllAssembliesInAppDomain() { AppDomain currentDomain = AppDomain.CurrentDomain; Assembly[] loadAssemblies=currentDomain.GetAssemblies(); Console.WriteLine("Assemblies of domain {0}",currentDomain.FriendlyName); foreach (Assembly a in loadAssemblies) { Console.WriteLine("-> Name: {0}, Version {1}", a.GetName().Name, a.GetName().Version); } } static void ListAllAssembliesInAppDomain2() { AppDomain currentDomain = AppDomain.CurrentDomain; var subset = from a in currentDomain.GetAssemblies() orderby a.GetName().Name select a; Console.WriteLine("Assemblies of domain {0}", currentDomain.FriendlyName); foreach (Assembly a in subset) { Console.WriteLine("-> Name: {0}, Version {1}", a.GetName().Name, a.GetName().Version); } } //on load assembly to domain - will message on every new assembly, loaded to domain static void InitCurrentDomain() { AppDomain currentDomain = AppDomain.CurrentDomain; currentDomain.AssemblyLoad += (o, s) => { Console.WriteLine("{0} has been loaded!",s.LoadedAssembly.GetName().Name); }; } static void CreateNewAppDomain() { AppDomain currentDomain = AppDomain.CreateDomain("AnotherDomain"); //load assembly currentDomain.Load("CarLib.dll"); // will load assembly to domain // unload domain event currentDomain.DomainUnload += (o, s) => { Console.WriteLine("The second app domain has been unloaded"); }; // ListAllAssembliesInAppDomain(currentDomain); // unload domain AppDomain.Unload(currentDomain); // will unload all assemblies with domain } static void ListAllAssembliesInAppDomain(AppDomain ad) { var loadedAssemblies = from a in ad.GetAssemblies() orderby a.GetName().Name select a; Console.WriteLine("***Loaded Assemblies in {0} ***",ad.FriendlyName); foreach (Assembly a in loadedAssemblies) { Console.WriteLine("-> Name: {0}, Version {1}", a.GetName().Name, a.GetName().Version); } } } } |