MemberInfo.GetCustomAttributes メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
派生クラスでオーバーライドされると、このメンバーに適用されたカスタム属性を返します。
オーバーロード
| 名前 | 説明 |
|---|---|
| GetCustomAttributes(Boolean) |
派生クラスでオーバーライドされると、このメンバーに適用されるすべてのカスタム属性の配列を返します。 |
| GetCustomAttributes(Type, Boolean) |
派生クラスでオーバーライドされた場合、このメンバーに適用され、 Typeによって識別されるカスタム属性の配列を返します。 |
GetCustomAttributes(Boolean)
- ソース:
- MemberInfo.cs
- ソース:
- MemberInfo.cs
- ソース:
- MemberInfo.cs
- ソース:
- MemberInfo.cs
- ソース:
- MemberInfo.cs
派生クラスでオーバーライドされると、このメンバーに適用されるすべてのカスタム属性の配列を返します。
public:
abstract cli::array <System::Object ^> ^ GetCustomAttributes(bool inherit);
public abstract object[] GetCustomAttributes(bool inherit);
abstract member GetCustomAttributes : bool -> obj[]
Public MustOverride Function GetCustomAttributes (inherit As Boolean) As Object()
パラメーター
- inherit
- Boolean
true このメンバーの継承チェーンを検索して属性を検索する場合。それ以外の場合は false。 プロパティとイベントでは、このパラメーターは無視されます。
返品
このメンバーに適用されるすべてのカスタム属性を含む配列、または属性が定義されていない場合は要素が 0 の配列。
実装
例外
このメンバーは、リフレクションのみのコンテキストに読み込まれる型に属しています。 「方法: アセンブリを Reflection-Only コンテキストに読み込む」を参照してください。
カスタム属性の型を読み込めませんでした。
例
次の例では、カスタム属性を定義し、属性を MyClass.MyMethodに関連付け、実行時に属性を取得して結果を表示します。
using System;
using System.Reflection;
// Define a custom attribute with one named parameter.
[AttributeUsage(AttributeTargets.All)]
public class MyAttribute : Attribute
{
private string myName;
public MyAttribute(string name)
{
myName = name;
}
public string Name
{
get
{
return myName;
}
}
}
// Define a class that has the custom attribute associated with one of its members.
public class MyClass1
{
[MyAttribute("This is an example attribute.")]
public void MyMethod(int i)
{
return;
}
}
public class MemberInfo_GetCustomAttributes
{
public static void Main()
{
try
{
// Get the type of MyClass1.
Type myType = typeof(MyClass1);
// Get the members associated with MyClass1.
MemberInfo[] myMembers = myType.GetMembers();
// Display the attributes for each of the members of MyClass1.
for(int i = 0; i < myMembers.Length; i++)
{
Object[] myAttributes = myMembers[i].GetCustomAttributes(true);
if(myAttributes.Length > 0)
{
Console.WriteLine("\nThe attributes for the member {0} are: \n", myMembers[i]);
for(int j = 0; j < myAttributes.Length; j++)
Console.WriteLine("The type of the attribute is {0}.", myAttributes[j]);
}
}
}
catch(Exception e)
{
Console.WriteLine("An exception occurred: {0}", e.Message);
}
}
}
Imports System.Reflection
' Define a custom attribute with one named parameter.
<AttributeUsage(AttributeTargets.All)> Public Class MyAttribute
Inherits Attribute
Private myName As String
Public Sub New(ByVal name As String)
myName = name
End Sub
Public ReadOnly Property Name() As String
Get
Return myName
End Get
End Property
End Class
' Define a class that has the custom attribute associated with one of its members.
Public Class MyClass1
<MyAttribute("This is an example attribute.")> Public Sub MyMethod(ByVal i As Integer)
Return
End Sub
End Class
Public Class MemberInfo_GetCustomAttributes
Public Shared Sub Main()
Try
' Get the type of MyClass1.
Dim myType As Type = GetType(MyClass1)
' Get the members associated with MyClass1.
Dim myMembers As MemberInfo() = myType.GetMembers()
' Display the attributes for each of the members of MyClass1.
Dim i As Integer
For i = 0 To myMembers.Length - 1
Dim myAttributes As [Object]() = myMembers(i).GetCustomAttributes(False)
If myAttributes.Length > 0 Then
Console.WriteLine("The attributes for the member {0} are: ", myMembers(i))
Dim j As Integer
For j = 0 To myAttributes.Length - 1
Console.WriteLine("The type of the attribute is: {0}", myAttributes(j))
Next j
End If
Next i
Catch e As Exception
Console.WriteLine("An exception occurred: {0}.", e.Message)
End Try
End Sub
End Class
注釈
このメソッドは、プロパティとイベントの inherit パラメーターを無視します。 継承チェーンでプロパティとイベントの属性を検索するには、 Attribute.GetCustomAttributes メソッドの適切なオーバーロードを使用します。
こちらもご覧ください
適用対象
GetCustomAttributes(Type, Boolean)
- ソース:
- MemberInfo.cs
- ソース:
- MemberInfo.cs
- ソース:
- MemberInfo.cs
- ソース:
- MemberInfo.cs
- ソース:
- MemberInfo.cs
派生クラスでオーバーライドされた場合、このメンバーに適用され、 Typeによって識別されるカスタム属性の配列を返します。
public:
abstract cli::array <System::Object ^> ^ GetCustomAttributes(Type ^ attributeType, bool inherit);
public abstract object[] GetCustomAttributes(Type attributeType, bool inherit);
abstract member GetCustomAttributes : Type * bool -> obj[]
Public MustOverride Function GetCustomAttributes (attributeType As Type, inherit As Boolean) As Object()
パラメーター
- attributeType
- Type
検索する属性の種類。 この型に割り当て可能な属性のみが返されます。
- inherit
- Boolean
true このメンバーの継承チェーンを検索して属性を検索する場合。それ以外の場合は false。 プロパティとイベントでは、このパラメーターは無視されます。
返品
このメンバーに適用されるカスタム属性の配列。または、 attributeType に割り当て可能な属性が適用されていない場合は、要素が 0 の配列。
実装
例外
カスタム属性の型を読み込むことができません。
attributeTypeがnullの場合。
このメンバーは、リフレクションのみのコンテキストに読み込まれる型に属しています。 「方法: アセンブリを Reflection-Only コンテキストに読み込む」を参照してください。
例
次の例では、継承されていない 2 つのメンバーを持つ BaseClass という名前のクラスを定義します。 total という名前のスレッド静的フィールドと、 MethodA という名前の CLS 準拠でないメソッドです。
DerivedClassという名前のクラスは、BaseClassから継承し、そのMethodA メソッドをオーバーライドします。
DerivedClassのメンバーには属性が適用されません。 この例では、 DerivedClass のメンバーを反復処理して、 CLSCompliantAttribute 属性と ThreadStaticAttribute 属性のどちらが適用されているかを判断します。
inheritはtrueされているため、メソッドは、指定した属性のDerivedClassの継承階層を検索します。 この例の出力が示すように、 total フィールドは ThreadStaticAttribute 属性で修飾され、 MethodA メソッドは CLSCompliantAttribute 属性で修飾されます。
using System;
public class BaseClass
{
[ThreadStatic] public int total;
[CLSCompliant(false)] public virtual uint MethodA()
{
return (uint) 100;
}
}
public class DerivedClass : BaseClass
{
public override uint MethodA()
{
total++;
return 200;
}
}
public class Example
{
public static void Main()
{
Type t = typeof(DerivedClass);
Console.WriteLine("Members of {0}:", t.FullName);
foreach (var m in t.GetMembers())
{
bool hasAttribute = false;
Console.Write(" {0}: ", m.Name);
if (m.GetCustomAttributes(typeof(CLSCompliantAttribute), true).Length > 0) {
Console.Write("CLSCompliant");
hasAttribute = true;
}
if (m.GetCustomAttributes(typeof(ThreadStaticAttribute), true).Length > 0) {
Console.Write("ThreadStatic");
hasAttribute = true;
}
if (!hasAttribute)
Console.Write("No attributes");
Console.WriteLine();
}
}
}
// The example displays the following output:
// Members of DerivedClass:
// MethodA: CLSCompliant
// ToString: No attributes
// Equals: No attributes
// GetHashCode: No attributes
// typeof: No attributes
// .ctor: No attributes
// total: ThreadStatic
Public Class BaseClass
<ThreadStatic> Public total As Integer
<CLSCompliant(False)> Public Overridable Function MethodA() As UInt32
Return CUInt(100)
End Function
End Class
Public Class DerivedClass : Inherits BaseClass
Public Overrides Function MethodA() As UInt32
total += 1
Return 200
End Function
End Class
Module Example
Public Sub Main()
Dim t As Type = GetType(DerivedClass)
Console.WriteLine("Members of {0}:", t.FullName)
For Each m In t.GetMembers()
Dim hasAttribute As Boolean = False
Console.Write(" {0}: ", m.Name)
If m.GetCustomAttributes(GetType(CLSCompliantAttribute), True).Length > 0 Then
Console.Write("CLSCompliant")
hasAttribute = True
End If
If m.GetCustomAttributes(GetType(ThreadStaticAttribute), True).Length > 0 Then
Console.Write("ThreadStatic")
hasAttribute = True
End If
If Not hasAttribute Then
Console.Write("No attributes")
End If
Console.WriteLine()
Next
End Sub
End Module
' The example displays the following output:
' Members of DerivedClass:
' MethodA: CLSCompliant
' ToString: No attributes
' Equals: No attributes
' GetHashCode: No attributes
' GetType: No attributes
' .ctor: No attributes
' total: ThreadStatic
注釈
このメソッドは、プロパティとイベントの inherit パラメーターを無視します。 継承チェーンでプロパティとイベントの属性を検索するには、 Attribute.GetCustomAttributes メソッドの適切なオーバーロードを使用します。