CustomAttributes Sample: Demonstrates Implementing a Custom Attribute
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
Author custom attributes across the C++ and C# languages.
Building and Running the Sample
To build and run CustomAttributes using Visual Studio
- In the Visual Studio IDE, load the solution file CustomAttributes.sln.
- In Solution Explorer, right-click the CustomAttributes solution.
- On the shortcut menu, click Build Solution.
- 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
SomeStuffclass located in tABC.cpp as well as on the
MYStuffclass located in Author.cpp. In addition to the
Authorcustom attribute, the sample also applies the C++
ABCattribute to the
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
SomeStuffclass located in tABC.cpp, on the
MYStuffclass located in Author.cs, and on the
AAAclass 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.cpp In this source file, the
AAAclass is annotated by several overloads of
ABCas well as three .NET Framework security attributes.
- Author.cs In this source file, the
MYStuffclass is annotated with several overloads of both
- tABC.cpp In this source file, the
SomeClassclass is annotated with several overloads of both
Note To view the custom metadata inserted by the
Authorattributes, examine ABC.dll, Author.dll, and tABC.exe with ILDASM.EXE (found in the .NET Framework SDK).