Partager via


Custom Attribute Technology Sample

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

This sample demonstrates how to create a .NET Framework custom attribute and associate that attribute with a type or member. The sample also demonstrates how to use the types in the System.Reflection namespace to determine whether a type or member has a custom attribute associated with it.

For information about using the samples, see the following topics:

To build the sample using the command prompt

  1. Open a Command Prompt window and navigate to one of the language-specific subdirectories for the sample.

  2. Type msbuild CustomAttributeCS.sln or msbuild CustomAttributeVB.sln, depending on your choice of programming language.

To build the sample using Visual Studio

  1. Open Windows Explorer and navigate to one of the language-specific subdirectories for the sample.

  2. Double-click the icon for CustomAttributeCS.sln or CustomAttributeVB.sln, depending on your choice of programming language, to open the file in Visual Studio.

  3. On the Build menu, click Build Solution.

To run the sample

  1. Navigate to the directory that contains the built executable, using the command prompt.

  2. Type CustAttr.exe.

Remarks

The sample uses the following technologies and classes:

  • Custom Attributes

    • Attribute When defining a custom attribute, you must derive from Attribute The sample derives a type named MyAttribute from Attribute.

    • ObsoleteAttribute Used to mark a code element as obsolete. This informs users of the code that they should not rely on its availability in future versions.

  • Reflection

    • Type Used to get custom attributes for a type, as well as to get an array of MemberInfo objects for that type.

    • MemberInfo Used to get an array of attributes for a member.

For more information about creating custom attributes, see the comments in the source code files.

See Also

Reference

Attribute

ObsoleteAttribute

System.Reflection

Type

MethodInfo

AssemblyBuilderAccess

MethodAttributes

Concepts

Extending Metadata Using Attributes

Other Resources

Emitting Dynamic Methods and Assemblies

Dynamic Source Code Generation and Compilation