Basics of Reflection

As mentioned in Chapter 2, you can explore the internals of a given assembly using a process called reflection. You can find out what assemblies are loaded into your current application domain. You can discover what types reside in each assembly, and for any given type, the methods and properties exposed by the type. You can even execute a method or change a property value via reflection, even though you might not know the name of the method or property at compile time.

In this section, you'll see the basic code required for each of these operations. The code uses classes in the System.Reflection namespace, most notably the Assembly class, and each example assumes that the code module has an Imports statement to import System.Reflection.

Major classes needed to use reflection capabilities include the following:

  • Assembly—Contains members to examine an assembly's metadata and even manipulate the assembly
  • AppDomain—Contains information about the currently running application domain
  • Type—Gives access to information about a .NET type

After this section, you will also see an additional capability provided through reflection: dynamic loading. You'll see how to gain a reference to an assembly on the fly and generate an instance of a type within the assembly.

While the process of reflection is powerful and enables you to perform operations that would otherwise be impossible, you should be aware of the performance implications of using reflection heavily. ...

Get Professional Visual Basic 2012 and .NET 4.5 Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.