Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit focus mode

Apply attributes

  • 2023-03-15
Feedback

In this article

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#, specifyname=value for each optional parameter, wherename is the name of the property. In Visual Basic, specifyname:=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 initializeSystem.ObsoleteAttribute, you only need to reference it asObsolete.

Apply an attribute to a method

The following code example shows how to useSystem.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 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 FunctionEnd ClassClass Test    Public Shared Sub Main()        ' This generates a compile-time warning.        Dim i As Integer = Example.Add(2, 2)    End SubEnd Class

Apply attributes at the assembly level

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

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 theIL Disassembler (Ildasm.exe) or by creating a custom program to retrieve the attribute.

See also

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNo

In this article

Was this page helpful?

YesNo