日期:2013-08-04  浏览次数:20395 次

begin:
Posted by scott on 2004年11月10日

The .NET platform depends on metadata at design time, compile time, and execution time. This article will cover some theory of metadata, and demonstrate how to examine metadata at runtime using reflection against ASP.NET web controls.
Metadata is the life blood of the .NET platform. Many of the features we use everyday during design time, during compile time, and during execution time, rely on the presence of metadata. Metadata allows an assembly, and the types inside an assembly, to be self-describing. In this article, we will discuss metadata and look at some of types and methods used to programmatically inspect and consume metadata.

Metadata – Some History
Metadata is not new in software development. In fact, many of .NETs predecessors used metadata. COM+ for instance, kept metadata in type libraries, in the registry, and in the COM+ catalog. This metadata could describe if a component required a database transaction or if the component should participate in object pooling. However, since the COM runtime kept metadata in various locations, irregularities could occur. Also, the metadata could not fully describe a type, and was not extensible.

A compiler for the common language runtime (CLR) will generate metadata during compilation and store the metadata (in a binary format) directly into assemblies and modules to avoid irregularities. The CLR also allows metadata extensibility, meaning if you want to add custom metadata to a type or an assembly, there are mechanisms for doing so.

Metadata in .NET cannot be underestimated. Metadata allows us to write a component in C# and let another application use the metadata from Visual Basic .NET. The metadata description of a type allows the runtime to layout an object in memory, to enforce security and type safety, and ensure version compatibilities.

Metadata & Reflection
Reflection is the ability to read metadata at runtime. Using reflection, it is possible to uncover the methods, properties, and events of a type, and to invoke them dynamically. Reflection also allows us to create new types at runtime, but in the upcoming example we will be reading and invoking only.

Reflection generally begins with a call to a method present on every object in the .NET framework: GetType. The GetType method is a member of the System.Object class, and the method returns an instance of System.Type. System.Type is the primary gateway to metadata. System.Type is actually derived from another important class for reflection: the MemeberInfo class from the System.Reflection namespace. MemberInfo is a base class for many other classes who describe the properties and methods of an object, including FieldInfo, MethodInfo, ConstructorInfo, ParameterInfo, and EventInfo among others. As you might suspect from thier names, you can use these classes to inspect different aspects of an object at runtime.

Metadata : An Example
In the web form shown below we will allow the user to inspect and set any property of a web control using reflection techniques. We have a Panel control on the bottom of the form filled with an assortment of controls (in this case, a TextBox, a Button, a HyperLink, and a Label, although if you download the code you can drag any controls into the panel and watch the example work).



When the page initially loads we will loop through the Controls collection of the Panel to see what controls are available. We can then display the available controls in the ListBox and allow the user to select a control. The code to load the ListBox is shown below.


private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
PopulateControlList();
}
}

private void PopulateControlList()
{
foreach(Control c in Panel1.Controls)
{
if(c is WebControl)<