CHAPTER 15 TYPE REFLECTION, LATE BINDING, AND ATTRIBUTE-BASED PROGRAMMING
}
}
}
break;
// Try to load assembly.
try
{
asm = Assembly.Load(asmName);
DisplayTypesInAsm(asm);
}
catch
{
Console.WriteLine("Sorry, can't find assembly.");
}
} while (true);
}
Notice that the static Assembly.Load() method has been passed only the friendly name of the
assembly you are interested in loading into memory. Thus, if you wish to reflect over CarLibrary.dll,
you will need to copy the CarLibrary.dll binary to the \bin\Debug directory of the
ExternalAssemblyReflector application to run this program. Once you do, you will find output similar to
the following:
***** External Assembly Viewer *****
Enter an assembly to evaluate
or enter Q to quit: CarLibrary
***** Types in Assembly *****
->CarLibrary, Version=2.0.0.0, Culture=neutral, PublicKeyToken=33a2bc294331e8b9
Type: CarLibrary.MusicMedia
Type: CarLibrary.EngineState
Type: CarLibrary.Car
Type: CarLibrary.SportsCar
Type: CarLibrary.MiniVan
If you wish to make ExternalAssemblyReflector more flexible, you can update your code to load the
external assembly using Assembly.LoadFrom() rather than Assembly.Load(), like so:
try
{
asm = Assembly.LoadFrom(asmName);
DisplayTypesInAsm(asm);
}
By doing so, you can enter an absolute path to the assembly you wish to view (e.g.,
C:\MyApp\MyAsm.dll). Essentially, Assembly.LoadFrom() allows you to programmatically supply a
value. With this adjustment, you can now pass in a full path to your Console Application.
Thus, if CarLibrary.dll was located under C:\MyCode, you could enter the following:
571