7.1. Reflection

Assemblies (either an .exe or a .dll) can contain one or more modules, though they usually contain just one. In turn, these modules contain one or more types; types in .NET include classes, interfaces, arrays, structures, delegates, and enumerations. Types contain members: fields, methods, properties, events, and parameters. The .NET class library supplies objects that encapsulate all these entities, letting you load an assembly at runtime (over the network, if you need to) and inspect all types in that assembly. Using the introspective properties of these objects, you can determine if a class is abstract, a method is public, or how many class constructors are available. In fact, anything you can describe with VB.NET code can be discovered at runtime. This process is called reflection.

Reflection is one of the .NET Framework's core technologies. In addition to runtime type discovery, you can use it to create assemblies, modules, and types at runtime and persist them to disk as .exe or .dll files. JScript .NET uses reflection to build symbol tables. The .NET Framework SDK, in fact, contains C# source code for two compilers that are written using reflection: a LISP compiler and a compiler for a subset of C called MyC. Serialization and remoting, both major players in .NET, also rely heavily on reflection.

Get Object-Oriented Programming with Visual Basic .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.