コードの要素に属性を適用するには、次のプロセスを使用します。
新しい属性を定義するか、既存の.NET属性を使用します。
要素の直前に配置して、コード要素に属性を適用します。
各言語には、独自の属性構文があります。 C++ と C# では、属性は角かっこで囲まれ、要素から空白で区切られ、改行を含めることができます。 Visual Basicでは、属性は角括弧で囲み、同じ論理行に配置する必要があります。改行が必要な場合は、行継続文字を使用できます。
位置指定パラメーターと属性の名前付きパラメーターを指定します。
位置指定 パラメーターは必須であり、名前付きパラメーターの前に指定する必要があります。属性のコンストラクターの 1 つのパラメーターに対応します。 名前付き パラメーターは省略可能であり、属性の読み取り/書き込みプロパティに対応します。 C++ と C# では、省略可能な各パラメーターに を指定します。ここで、 はプロパティの名前です。 Visual Basicで、
name:=valueを指定します。
この属性は、コードをコンパイルするときにメタデータに出力され、ランタイム リフレクション サービスを介して共通言語ランタイムおよびカスタム ツールまたはアプリケーションで使用できます。
慣例により、すべての属性名は "Attribute" で終わっています。 ただし、Visual Basicや C# など、ランタイムを対象とする複数の言語では、属性の完全な名前を指定する必要はありません。 たとえば、 を初期化する場合は、 古い形式として参照するだけで済みます。
有効な属性引数
属性に引数を渡す場合は、次のいずれかの式を使用します。
- 定数式 (リテラル、値、および列挙値)。
- 型式 (C# では
typeof、Visual Basic ではGetType)。 - コンパイル時に文字列定数を生成する名前式 (C# では
nameof、Visual Basic ではNameOf)。 - 前の式のみを要素値として使用する属性パラメーター型の配列作成式。
次の型は、属性パラメーターの型として有効です。
単純型 (C# キーワード/Visual Basic キーワード/.NETランタイム型):
C# Visual Basic .NETランタイムの種類 boolBooleanBoolean byteByteByte charCharChar doubleDoubleDouble floatSingleSingle intIntegerInt32 longLongInt64 shortShortInt16 stringStringString (C# では、値が有効な属性引数の型のいずれか、またはその 1 次元配列である場合)。
。
使用サイトでアクセスできる列挙型。
上記のいずれかの型の 1 次元配列。
注
リテラル定数をサポートしていても、 、 、 、 、 、 、および の型は有効な属性パラメーター型ではありません。
有効な属性引数の例を次に示します。
[MyAttr(true)] // bool literal
[MyAttr(42)] // int literal
[MyAttr("hello")] // string literal
[MyAttr(MyEnum.Value)] // enum value
[MyAttr(typeof(string))] // typeof expression
[MyAttr(nameof(MyClass))] // nameof expression (string constant)
[MyAttr(new int[] { 1, 2, 3 })] // array of constants
[MyAttr(new string[] { "a", "b" })] // array of strings
<MyAttr(True)> ' Boolean literal
<MyAttr(42)> ' Integer literal
<MyAttr("hello")> ' String literal
<MyAttr(MyEnum.Value)> ' Enum value
<MyAttr(GetType(String))> ' GetType expression
<MyAttr(NameOf(MyClass))> ' NameOf expression (string constant)
<MyAttr(New Integer() {1, 2, 3})> ' Array of constants
次の例は、コンパイラ エラーの原因となる引数を示しています。
string value = "test";
[MyAttr(value)] // Error CS0182: not a constant expression
[MyAttr(GetValue())] // Error CS0182: method calls aren't allowed
メソッドに属性を適用する
次のコード例は、コードを古いものとしてマークする の使用方法を示しています。 文字列が属性に渡されます。 この属性は、属性が記述するコードが呼び出されたときに渡された文字列を表示するコンパイラ警告を発生させます。
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
アセンブリ レベルで属性を適用する
アセンブリ レベルで属性を適用する場合は、assembly (Visual Basic の Assembly) キーワードを使用します。 次のコードは、アセンブリ レベルで適用 を示しています。
using System.Reflection;
[assembly:AssemblyTitle("My Assembly")]
Imports System.Reflection
<Assembly: AssemblyTitle("My Assembly")>
この属性を適用すると、 文字列はファイルのメタデータ部分のアセンブリ マニフェストに配置されます。 属性を表示するには、 IL 逆アセンブラー (Ildasm.exe) を使用するか、属性を取得するカスタム プログラムを作成します。
こちらも参照ください
- 属性
- 属性に格納されている情報の取得
- 概念
- 属性 (C#)
- Attributes の概要 (Visual Basic)
.NET