Team LiB
Previous Section Next Section

Chapter 11. Reflection and Code Attributes

IN BRIEF

Managed code written for the .NET Framework is always accompanied by various kinds of metadata. This metadata controls everything from defining the list of referenced assemblies that an application needs to run properly, to simpler information such as the product version, product name, and so on.

This metadata is stored on disk, but it is also available to managed code at runtime. While the managed code is running, it can, at any time, make queries against the metadata associated with it. In addition to this metadata, type information including class definitions, members, properties, attributes, and instance data is also available.

The mechanism that makes all of this available is called reflection. Reflection allows managed code to not only inspect metadata about itself, but also about any other code that might be running at the same time. This chapter will show you how to use reflection to discover type and metadata information at runtime, and how to use custom code attributes to create your own runtime-discoverable metadata about your code.

WHAT YOU NEED

RECOMMENDED SOFTWARE

.NET Framework SDK v1.1

Visual Studio .NET 2003 with C# installed

RECOMMENDED HARDWARE

PC that meets .NET SDK minimum requirements

SKILLS REQUIRED

C# and .NET familiarity


REFLECTION AND CODE ATTRIBUTES AT A GLANCE

Working with Reflection

228

  
 

Introduction to Reflection

229

Discovering Type Information at Runtime

229

 

How Reflection Works

229

Using Code Attributes

234

  
 

Introduction to Code Attributes

234

Creating Custom Attributes

236

 

Using Code Attributes

235

Querying Custom Attributes

238

Summary

241

  

Further Reading

241

  


    Team LiB
    Previous Section Next Section