Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

CustomAttributes Sample: Demonstrates Implementing a Custom Attribute

Visual Studio .NET 2003

The CustomAttributes sample illustrates the Reflection class. Custom attributes can be used to create type-checked user-defined metadata. This sample demonstrates this technique by implementing the ABC and Author custom attributes across the C++ and C# languages.

Building and Running the Sample

To build and run CustomAttributes using Visual Studio

  1. In the Visual Studio IDE, load the solution file CustomAttributes.sln.
  2. In Solution Explorer, right-click the CustomAttributes solution.
  3. On the shortcut menu, click Build Solution.
  4. On the Debug menu, click Start.

Custom Attributes in the Sample

The custom attributes are as follows:

  • Author (implemented with C#)

    This custom attribute adds several different types of information to the metadata of the target class and defines several different parameter lists for usage. It inherits directly from System::Attribute. The sample demonstrates each of these versions on the SomeStuff class located in tABC.cpp as well as on the MYStuff class located in Author.cpp. In addition to the Author custom attribute, the sample also applies the C++ ABC attribute to the SomeStuff class.

  • ABC (implemented with C++)

    This custom attribute adds several different types of information to the metadata of the target class, such as enumerated types, integers, floats, and strings. Similar to Author, several different parameter lists are defined for usage. The sample demonstrates each of these versions on the SomeStuff class located in tABC.cpp, on the MYStuff class located in Author.cs, and on the AAA class located in ABC.cpp.

Files in the Sample

The files in the sample are as follows:

  • ABC.dll   Implements the overloaded custom attribute ABC, which is imported by Author.dll (written in C#).
  • Author.dll   Implements an overloaded custom attribute Author, imported by the tABC application along with ABC.
  • ABC.cpp   In this source file, the AAA class is annotated by several overloads of ABC as well as three .NET Framework security attributes.
  • Author.cs   In this source file, the MYStuff class is annotated with several overloads of both ABC and Author.
  • tABC.cpp   In this source file, the SomeClass class is annotated with several overloads of both ABC and Author.
Note   To view the custom metadata inserted by the ABC and Author attributes, examine ABC.dll, Author.dll, and tABC.exe with ILDASM.EXE (found in the .NET Framework SDK).

See Also

Managed Extensions for C++ Samples

Show:
© 2015 Microsoft