Free mag vol1 | Page 630

CHAPTER 15  TYPE REFLECTION, LATE BINDING, AND ATTRIBUTE-BASED PROGRAMMING ***** External Assembly Viewer ***** Enter an assembly to evaluate or enter Q to quit: C:\MyCode\CarLibrary.dll ***** Types in Assembly ***** ->CarLibrary, Version=2.0.0.0, Culture=neutral, PublicKeyToken=33a2bc294331e8b9 Type: CarLibrary.EngineState Type: CarLibrary.Car Type: CarLibrary.SportsCar Type: CarLibrary.MiniVan  Source Code The ExternalAssemblyReflector project is included in the Chapter 15 subdirectory. Reflecting on Shared Assemblies The Assembly.Load() method has been overloaded a number of times. One variation allows you to specify a culture value (for localized assemblies), as well as a version number and public key token value (for shared assemblies). Collectively speaking, the set of items identifying an assembly is termed the display name. The format of a display name is a comma-delimited string of name/value pairs that begins with the friendly name of the assembly, followed by optional qualifiers (that may appear in any order). Here is the template to follow (optional items appear in parentheses): Name (,Version = major.minor.build.revision) (,Culture = culture token) (,PublicKeyToken= public key token) When you’re crafting a display name, the convention PublicKeyToken=null indicates that binding and matching against a non–strongly named assembly is required. Additionally, Culture="" indicates matching against the default culture of the target machine, for example: // Load version 1.0.0.0 of CarLibrary using the default cu lture. Assembly a = Assembly.Load(@"CarLibrary, Version=1.0.0.0, PublicKeyToken=null, Culture="""); Also be aware that the System.Reflection namespace supplies the AssemblyName type, which allows you to represent the preceding string information in a handy object variable. Typically, this class is used in conjunction with System.Version, which is an OO wrapper around an assembly’s version number. Once you have established the display name, it can then be passed into the overloaded Assembly.Load() method, like so: // Make use of AssemblyName to define the display name. AssemblyName asmName; asmName = new AssemblyName(); asmName.Name = "CarLibrary"; Version v = new Version("1.0.0.0"); asmName.Version = v; Assembly a = Assembly.Load(asmName); 572