Поделиться через


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. Initialize the attribute directly preceding the element that you want described, calling the attribute's constructor with the desired flag or information.

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.

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 MainApp
{
    public static void Main()
    {
        //This generates a compile-time warning.
        int MyInt = Add(2,2); 
    }
    //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);
    }  
}

[C++]
#using <mscorlib.dll>
using namespace System;
int Add(int a, int b);   
void main(void)
{
    //This generates a compile-time warning.
    int MyInt = Add(2, 2);
    return;
}
//Specify attributes between square brackets in C++.
//This attribute is applied only to the Add method.
[Obsolete("Will be removed in next version ")]
Add( int a, int b)
{
    return (a+b);
}

[Visual Basic]
Imports System 
'Call attributes between < and > in Visual Basic.
Public Module main
    Sub Main()
    'This generates a compile-time warning.
    Dim MyInt as Integer = Add(2,2)
    End Sub
    'Specify attributes between < and > brackets in Visual Basic.
    'This attribute is applied only to the Add method.
    
<Obsolete("Will be removed in next version ")> Function Add(a as Integer, b as Integer) as Integer
        Add = a + b
    End Function
End Module 

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")]
[C++]
using namespace System::Reflection;
[assembly:AssemblyName("MyAssembly")]
[Visual Basic]
Imports 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

Extending Metadata Using Attributes | Retrieving Information Stored in Attributes