Redigeeri

Jagamisviis:


Apply attributes

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

  1. Define a new attribute or use an existing .NET attribute.

  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.

  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++, and C#, 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.

Apply an attribute to a method

The following code example shows how to use 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.

public ref class Example
{
    // Specify attributes between square brackets in C#.
    // This attribute is applied only to the Add method.
public:
    [Obsolete("Will be removed in next version.")]
    static int Add(int a, int b)
    {
        return (a + b);
    }
};

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

int main()
{
    Test::Main();
}
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
{
    public static void Main()
    {
        // This generates a compile-time warning.
        int i = Example.Add(2, 2);
    }
}
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 Shared Function Add(a As Integer, b As Integer) As Integer
        Return a + b
    End Function
End Class

Class Test
    Public Shared Sub Main()
        ' This generates a compile-time warning.
        Dim i As Integer = Example.Add(2, 2)
    End Sub
End Class

Apply attributes at the assembly level

If you want to apply an attribute at the assembly level, use the assembly (Assembly in Visual Basic) keyword. The following code shows the AssemblyTitleAttribute applied at the assembly level.

using namespace System::Reflection;
[assembly:AssemblyTitle("My Assembly")];
using System.Reflection;
[assembly:AssemblyTitle("My Assembly")]
Imports System.Reflection
<Assembly: AssemblyTitle("My Assembly")>

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

See also