Free mag vol1 | Page 631

CHAPTER 15  TYPE REFLECTION, LATE BINDING, AND ATTRIBUTE-BASED PROGRAMMING To load a shared assembly from the GAC, the Assembly.Load() parameter must specify a PublicKeyToken value. For example, assume you have a new Console Application named SharedAsmReflector, and wish to load version 4.0.0.0 of the System.Windows.Forms.dll assembly provided by the .NET base class libraries. Given that the number of types in this assembly is quite large, the following application prints out only the names of public enums, using a simple LINQ query: using using using using System; System.Collections.Generic; System.Linq; System.Text; using System.Reflection; using System.IO; namespace SharedAsmReflector { public class SharedAsmReflector { private static void DisplayInfo(Assembly a) { Console.WriteLine("***** Info about Assembly *****"); Console.WriteLine("Loaded from GAC? {0}", a.GlobalAssemblyCache); Console.WriteLine("Asm Name: {0}", a.GetName().Name); Console.WriteLine("Asm Version: {0}", a.GetName().Version); Console.WriteLine("Asm Culture: {0}", a.GetName().CultureInfo.DisplayName); Console.WriteLine("\nHere are the public enums:"); // Use a LINQ query to find the public enums. Type[] types = a.GetTypes(); var publicEnums = from pe in types where pe.IsEnum && pe.IsPublic select pe; } foreach (var pe in publicEnums) { Console.WriteLine(pe); } static void Main(string[] args) { Console.WriteLine("***** The Shared Asm Reflector App *****\n"); // Load System.Windows.Forms.dll from GAC. string displayName = null; displayName = "System.Windows.Forms," + "Version=4.0.0.0," + "PublicKeyToken=b77a5c561934e089," + @"Culture="""; Assembly asm = Assembly.Load(displayName); DisplayInfo(asm); Console.WriteLine("Done!"); 573