Simplest Example


As always, I’ve written the simplest example I could think of to demonstrate .NET reflection. You can download it from this book’s Web site, http://www.introducingmicrosoft.net, and work along with me. Unlike the other chapters, I use the same sample program throughout this chapter. In this section, I discuss opening an assembly and reading information about the assembly itself. Later sections will discuss examining the types inside the assembly and creating and using objects based on those types. Figure 11-3 shows a screen shot of the sample program.

A simple reflection example starts here.

click to expand
Figure 11-3: The reflection sample program

The system-provided code for .NET reflection lives in the System namespace. Even though your own code won’t use it that often, you can see how vital reflection is to the system by the fact that it occupies space in the main system area. The names of most reflection objects live in the namespace System.Reflection, with the notable exception of System.Type (which will be discussed more later).

Here’s where the system reflection code lives.

I want to open an assembly and display information about it, such as its version. These properties are members of the System.Reflection.Assembly class, which represents (you guessed it) an assembly. There are many different ways in which a programmer can obtain this object, most of which use static members of this class. For example, the Assembly.GetExecutingAssembly method fetches a reference to the assembly in which the calling code is running, and the Assembly.GetCallingAssembly method gets the assembly of the client that called a function (with which you can work your way up the call chain to find the root caller, as the code access security system does). When the user clicks Select Assembly, I pop up a dialog box that allows the user to select a file. I then load the assembly from the selected file via the Assembly.LoadFrom method. The System.Reflection.Assembly class contains several other Load(something) methods for handling different scenarios. I then enumerate the assembly’s contents to display them on the tree control, which I’ll discuss in later sections of this chapter.

You obtain an assembly object in several different ways.

Once I’ve opened my assembly object, I want to display its properties. When the user selects the assembly in the tree control and clicks Explore Selected Member, I pop up a dialog box that displays some of the interesting properties of the assembly, as shown in Figure 11-4. The code is shown in Listing 11-1. I show the assembly’s name and its codebase, whether or not it came from the global assembly cache (GAC), the assemblies that it references, and its custom attributes. That’s all I have to do to see what’s in an assembly.

A System.Reflection.Assembly object contains information describing the assembly.

click to expand
Figure 11-4: Dialog box showing properties of an assembly

Listing 11-1: Code that reads metadata properties of an assembly

start example
Public Sub DisplayAssemblyProperties(ByVal ThisAssembly As _ System.Reflection.Assembly) ’ Show assembly’s name Label2.Text = ThisAssembly.FullName ’ Show code base Label4.Text = ThisAssembly.CodeBase ’ Show if it’s from the GAC or not CheckBox1.Checked = ThisAssembly.GlobalAssemblyCache ’ Show the references Dim RefName As System.Reflection.AssemblyName For Each RefName In ThisAssembly.GetReferencedAssemblies() ListBox1.Items.Add(RefName) Next ’ Show the custom attributes Dim Attr As Object For Each Attr In ThisAssembly.GetCustomAttributes(True) ListBox2.Items.Add(Attr) Next End Sub
end example




Introducing Microsoft. NET
Introducing Microsoft .NET (Pro-Developer)
ISBN: 0735619182
EAN: 2147483647
Year: 2003
Pages: 110

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net