Freigeben über


Applying Attributes 

Use the following process to apply an attribute to an element of your code.

  1. Define a new attribute or use an existing attribute by importing its namespace from the .NET Framework.

  2. Apply the attribute to the code element by placing it immediately before the element.

    Each language has its own attribute syntax. In C++ and C#, the attribute is surrounded by square brackets and separated from the element by white space, which can include a line break. In Visual Basic, the attribute is surrounded by angle brackets and must be on the same logical line; the line continuation character can be used if a line break is desired. In J#, the attribute is attached using special comment syntax.

  3. Specify positional parameters and named parameters for the attribute.

    Positional parameters are required and must come before any named parameters; they correspond to the parameters of one of the attribute's constructors. Named parameters are optional and correspond to read/write properties of the attribute. In C++, C#, and J#, specify name=value for each optional parameter, where name is the name of the property. In Visual Basic, specify name:=value.

The attribute is emitted into metadata when you compile your code and is available to the common language runtime and any custom tool or application through the runtime reflection services.

By convention, all attribute names end with Attribute. However, several languages that target the runtime, such as Visual Basic and C#, do not require you to specify the full name of an attribute. For example, if you want to initialize System.ObsoleteAttribute, you only need to reference it as Obsolete.

Applying an Attribute to a Method

The following code example shows how to declare System.ObsoleteAttribute, which marks code as obsolete. The string "Will be removed in next version" is passed to the attribute. This attribute causes a compiler warning that displays the passed string when code that the attribute describes is called.

using System;
public class Example
{
    // Specify attributes between square brackets in C#.
    // This attribute is applied only to the Add method.
    [Obsolete("Will be removed in next version.")]
    public static int Add(int a, int b)
    {
        return (a + b);
    }
}
class Test
{
    static void Main()
    {
        // This generates a compile-time warning.
        int i = Example.Add(2, 2);
    }
}
using namespace System;
public ref class Example
{
public:
    // Specify attributes between square brackets in C++.
    // This attribute is applied only to the Add method.
    [Obsolete("Will be removed in next version ")]
    static int Add(int a, int b)
    {
        return (a + b);
    }
};
void main()
{
    // This generates a compile-time warning.
    int i = Example::Add(2, 2);
    return;
}
Imports System 
Public Class Example
    ' Specify attributes between angle brackets in Visual Basic,
    ' and keep them on the same logical line.
    ' This attribute is applied only to the Add method.
    <Obsolete("Will be removed in next version ")> _
    Public Shared Function Add(ByVal a As Integer, ByVal b As Integer) As Integer
        Return a + b
    End Function
End Class
Module Test
    Sub Main()
        ' This generates a compile-time warning.
        Dim i As Integer = Example.Add(2, 2)
    End Sub
End Module
import System.*;
public class Example
{
    // Specify attributes with comment syntax in J#.
    // This attribute is applied only to the Add method.
    /** @attribute Obsolete("Will be removed in next version") */
    public static int Add(int a, int b)
    {
        return (a + b);
    }
}

class Test
{ 
    public static void main()
    {
        // This generates a compile-time warning.
        int MyInt = Example.Add(2,2); 
    }
}

Applying Attributes at the Assembly Level

If you want to apply an attribute at the assembly level, use the Assembly keyword. The following code shows the AssemblyNameAttribute applied at the assembly level.

using System.Reflection;
[assembly:AssemblyName("MyAssembly")]
using namespace System::Reflection;
[assembly:AssemblyName("MyAssembly")]
Imports System.Reflection
<Assembly:AssemblyName("MyAssembly")> 
import System.Reflection.*;
/** @assembly AssemblyName("MyAssembly") */

When this attribute is applied, the string "MyAssembly" is placed in the assembly manifest in the metadata portion of the file. You can view the attribute either by using the MSIL Disassembler (Ildasm.exe) or by creating a custom program to retrieve the attribute.

See Also

Reference

Using Attributes (C# Programming Guide)
Attaching Attributes

Concepts

Retrieving Information Stored in Attributes
Application of Attributes

Other Resources

Extending Metadata Using Attributes
Attributed Programming Concepts