Metadata and Attributes


The concept of metadata is central to the way the .NET Framework works, so to be an effective .NET programmer, you need to know what it is and how to work with it. Metadata is data attached to .NET data types that carries information about those types. A lot of metadata contains information that can’t be specified in the programming language, and it offers a useful—and many people would say essential—way to provide all the extra information needed by the .NET run time.

One of the major advantages of metadata is that it is stored along with the code, so extra data doesn’t need to be stored separately. For example, with COM objects, all extra data has to be stored in the Microsoft Windows registry. One of the main problems with configuring and using COM objects is ensuring that the data in the registry doesn’t get corrupted or become out of step with the COM code.

Another major advantage of metadata is that it provides a way to add version information to the code so that you know which version of a component you’re using. This solves a lot of problems that have plagued programmers since the early days of Windows, and is a huge step forward.

The compiler always attaches metadata to the output code to describe it, and the common language runtime (CLR) uses the metadata to control the loading and execution of the code. You can also attach metadata to code using attributes, which are special syntax elements that can be attached to classes and class members. You’ll see how to use attributes later in this chapter.

You can see some of the metadata that the compiler attaches to your code if you use the ISDASM (the IL disassembler tool), which is included with the .NET Framework SDK. (You can find this tool in \Program Files\Microsoft Visual Studio .NET\FrameworkSDK\bin, if you’ve installed to the default location.) The following example shows you how to use ILDASM to examine a simple program:

  1. Create a new Visual C++ Console Application (.NET) project called Hello.

  2. Add a new managed class to the program:

    __gc class Hello { public: static void SayHello() { Console::WriteLine(S"Hello, world"); } };

    The class doesn’t really have to do anything particular; it is simply here so that you can disassemble it to look at the metadata.

  3. Build the program to generate the executable.

  4. Run the ILDASM program. This program doesn’t appear on the Start menu, so you need to run it from the command line. To do this, start a Microsoft Visual Studio .NET command prompt, which has the path set up to let you run all the tools from the Visual Studio and .NET Framework directories. You’ll find the Microsoft Visual Studio .NET 2003 Command Prompt on the Start menu under Programs, Microsoft Visual Studio .NET 2003, Visual Studio .NET Tools. Start a command prompt and type ildasm to run the tool. You should see a window like this:

    click to expand

  5. Select Open from the ILDASM File menu, navigate to the Hello.exe executable, and open it. The display should now look like this:

    click to expand

  6. We’re interested in the managed type Hello, which is indicated by the blue component symbol. Click on the plus sign (+) to expand the tree for Hello and display the details of the class:

    The type has three entries: the details of the class and the entries for two methods, which are the SayHello method you added and the default constructor provided by the compiler.

  7. Double-click on the red triangle to bring up the class information. You’ll see a window like this:

    click to expand

The definition of the managed class—which extends System::Object—is marked as private auto ansi. These keywords represent items of metadata that have been added by the compiler to describe the class. You can open the other methods in this class to see what metadata is attached to them.

You can inquire about attributes at run time using reflection, a feature of the Microsoft Windows .NET Framework 1.1 that allows programmers to find out information about the objects they are using, such as what class the objects belong to, what methods the objects support, and what metadata is attached to them. Using attributes in code is very powerful because it gives you a way to extend the programming language, introducing new properties for your classes that don’t exist in the base language.

Later in the chapter, you’ll see how to create custom attributes and how to use code to look at the attributes attached to classes.




Microsoft Visual C++  .NET(c) Step by Step
Microsoft Visual C++ .NET(c) Step by Step
ISBN: 735615675
EAN: N/A
Year: 2003
Pages: 208

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net