|
2 | 2 | title: "Applying Attributes" |
3 | 3 | ms.date: "03/30/2017" |
4 | 4 | ms.technology: dotnet-standard |
5 | | -dev_langs: |
| 5 | +dev_langs: |
6 | 6 | - "csharp" |
7 | 7 | - "vb" |
8 | 8 | - "cpp" |
9 | | -helpviewer_keywords: |
| 9 | +helpviewer_keywords: |
10 | 10 | - "assemblies [.NET Framework], attributes" |
11 | 11 | - "attributes [.NET Framework], applying" |
12 | 12 | ms.assetid: dd7604eb-9fa3-4b60-b2dd-b47739fa3148 |
13 | 13 | --- |
14 | | -# Applying Attributes |
15 | | -Use the following process to apply an attribute to an element of your code. |
16 | | - |
17 | | -1. Define a new attribute or use an existing attribute by importing its namespace from the .NET Framework. |
18 | | - |
19 | | -2. Apply the attribute to the code element by placing it immediately before the element. |
20 | | - |
| 14 | +# Apply attributes |
| 15 | + |
| 16 | +Use the following process to apply an attribute to an element of your code. |
| 17 | + |
| 18 | +1. Define a new attribute or use an existing .NET attribute. |
| 19 | + |
| 20 | +2. Apply the attribute to the code element by placing it immediately before the element. |
| 21 | + |
21 | 22 | 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. |
22 | | - |
23 | | -3. Specify positional parameters and named parameters for the attribute. |
24 | | - |
25 | | - 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`. |
26 | | - |
27 | | - 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. |
28 | | - |
29 | | - 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 <xref:System.ObsoleteAttribute?displayProperty=nameWithType>, you only need to reference it as **Obsolete**. |
30 | | - |
31 | | -## Applying an Attribute to a Method |
32 | | - 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. |
33 | | - |
| 23 | + |
| 24 | +3. Specify positional parameters and named parameters for the attribute. |
| 25 | + |
| 26 | + *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`. |
| 27 | + |
| 28 | + 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. |
| 29 | + |
| 30 | + 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 <xref:System.ObsoleteAttribute?displayProperty=nameWithType>, you only need to reference it as **Obsolete**. |
| 31 | + |
| 32 | +## Apply an attribute to a method |
| 33 | + |
| 34 | + 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. |
| 35 | + |
34 | 36 | [!code-cpp[Conceptual.Attributes.Usage#3](../../../samples/snippets/cpp/VS_Snippets_CLR/conceptual.attributes.usage/cpp/source1.cpp#3)] |
35 | 37 | [!code-csharp[Conceptual.Attributes.Usage#3](../../../samples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/source1.cs#3)] |
36 | | - [!code-vb[Conceptual.Attributes.Usage#3](../../../samples/snippets/visualbasic/VS_Snippets_CLR/conceptual.attributes.usage/vb/source1.vb#3)] |
37 | | - |
38 | | -## Applying Attributes at the Assembly Level |
39 | | - 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. |
40 | | - |
| 38 | + [!code-vb[Conceptual.Attributes.Usage#3](../../../samples/snippets/visualbasic/VS_Snippets_CLR/conceptual.attributes.usage/vb/source1.vb#3)] |
| 39 | + |
| 40 | +## Apply attributes at the assembly level |
| 41 | + |
| 42 | + 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. |
| 43 | + |
41 | 44 | [!code-cpp[Conceptual.Attributes.Usage#2](../../../samples/snippets/cpp/VS_Snippets_CLR/conceptual.attributes.usage/cpp/source1.cpp#2)] |
42 | 45 | [!code-csharp[Conceptual.Attributes.Usage#2](../../../samples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/source1.cs#2)] |
43 | | - [!code-vb[Conceptual.Attributes.Usage#2](../../../samples/snippets/visualbasic/VS_Snippets_CLR/conceptual.attributes.usage/vb/source1.vb#2)] |
44 | | - |
45 | | - 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 [MSIL Disassembler (Ildasm.exe)](../../framework/tools/ildasm-exe-il-disassembler.md) or by creating a custom program to retrieve the attribute. |
46 | | - |
| 46 | + [!code-vb[Conceptual.Attributes.Usage#2](../../../samples/snippets/visualbasic/VS_Snippets_CLR/conceptual.attributes.usage/vb/source1.vb#2)] |
| 47 | + |
| 48 | + 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 [MSIL Disassembler (Ildasm.exe)](../../framework/tools/ildasm-exe-il-disassembler.md) or by creating a custom program to retrieve the attribute. |
| 49 | + |
47 | 50 | ## See also |
48 | 51 |
|
49 | 52 | - [Attributes](index.md) |
|
0 commit comments