Share via


Metadata API Overview

The common language runtime metadata APIs allow a component's metadata to be accessed without requiring the class to be loaded by the runtime. The APIs are designed specifically to maximize performance and minimize overhead — the metadata engine makes the data available but stops short of providing direct access to the in-memory data structures. In contrast, when a class is loaded at run time, the loader imports the metadata into its own data structures, which you can browse using the runtime's Reflection services.

Metadata APIs versus Reflection Services

The Reflection services do much more work than the metadata APIs do, such as automatically walking the inheritance hierarchy to obtain information about inherited methods and fields. The metadata APIs return only the direct member declarations for a given class and require the API client to make additional calls to walk the hierarchy and enumerate inherited methods. The Reflection services approach exposes a higher-level view of metadata, where the metadata API approach puts the API client in complete control of walking the data structures.

Scope

At any given time you might have several distinct areas of memory containing metadata. For example, you might have one area that maps all of the metadata from an existing module on disk. At the same time, you might be emitting metadata into a separate area that you will later save as a module into a file.

Note

The word module here means a file that contains metadata. Typically it will be an .obj, .exe, or .dll file that contains both metadata and MSIL code, but it can also be a file containing only metadata.

Each separate area of metadata in memory is referred to as a scope. Each scope corresponds to one module. Modules are often saved as files on disk, but this is not required. For example, scripting tools frequently generate metadata that is never persisted into a file.

The term "scope" is used because it represents the region within which metadata tokens are defined. For example, a metadata token with value N identifies details about a class definition, within a given scope. But a metadata token with that same value N might correspond to a completely different set of details for a different scope.

To establish a metadata scope in memory, you call the CComPtrBase::CoCreateInstance method of for the IMetaDataDispenser Interface interface, creates a new scope or opens an existing set of metadata structures from a file or memory location. With each call to the IMetaDataDispenser::DefineScope Method or the IMetaDataDispenser::OpenScope Method method, the caller specifies which API to receive:

Error Checking

The metadata APIs perform a minimum of semantic error checking. The metadata API methods assume that the tools and services that emit metadata are enforcing the object system rules outlined in the common type system, and that any additional checking by the metadata engine during development time is superfluous.

See Also

Other Resources

Overview of Metadata