指定されている型のカスタム属性がこのメンバに定義されているかどうかを確認します。
Public Overridable Function IsDefined( _
ByVal attributeType As Type, _ ByVal inherit As Boolean _) As Boolean Implements ICustomAttributeProvider.IsDefined
[C#]
public virtual bool IsDefined(TypeattributeType,boolinherit);
[C++]
public: virtual bool IsDefined(Type* attributeType,boolinherit);
[JScript]
public function IsDefined(
attributeType : Type,inherit : Boolean) : Boolean;
パラメータ
- attributeType
検索する Type オブジェクト。 - inherit
この型のオブジェクトでは、この引数は無視されます。
戻り値
このメンバに attributeType のインスタンスが 1 つ以上定義されている場合は true 。それ以外の場合は false 。
実装
ICustomAttributeProvider.IsDefined
例外
例外の種類 | 条件 |
---|---|
ArgumentNullException | attributeType が null 参照 (Visual Basic では Nothing) です。 |
使用例
' The following example shows how the attributes related to the parameters of methods
' of the class can be retrieved at run time. A custom parameter level attribute
' named 'MyAttribute' is defined. This custom attribute is associated with a
' parameter of the method 'MyMethod' of 'MyClass1'. This attribute is then
' retrieved at run time for the parameter of the method of the class 'MyClass1'
' and displayed.
Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic
' Define a custom attribute with one named parameter.
<AttributeUsage(AttributeTargets.Parameter)> Public Class MyAttribute
Inherits Attribute
Private myName As String
Public Sub New(ByVal name As String)
myName = name
End Sub 'New
Public ReadOnly Property Name() As String
Get
Return myName
End Get
End Property
End Class 'MyAttribute
' Define a class which has a custom attribute associated with one of parameters of a method.
Public Class MyClass1
Public Sub MyMethod(<MyAttribute("This is an example parameter attribute")> ByVal i As Integer)
Return
End Sub 'MyMethod
End Class 'MyClass1
Public Class MemberInfo_GetCustomAttributes
Public Shared Sub Main()
' Get the type of the class 'MyClass1'.
Dim myType As Type = GetType(MyClass1)
' Get the members associated with the class 'MyClass1'.
Dim myMethods As MethodInfo() = myType.GetMethods()
' Display the attributes of type 'MyAttribute' for each of the parameters of each method of the class 'MyClass1'.
Dim i As Integer
For i = 0 To myMethods.Length - 1
' Get the parameters for the method.
Dim myParameters As ParameterInfo() = myMethods(i).GetParameters()
Console.WriteLine(ControlChars.Cr & "The parameters for the method {0} that have a custom attribute are : " & ControlChars.Cr, myMethods(i))
Dim j As Integer
For j = 0 To myParameters.Length - 1
' Get the attributes of type 'MyAttribute' for each parameter.
Dim myAttributes As [Object]() = myParameters(j).GetCustomAttributes(GetType(MyAttribute), False)
If myParameters(j).IsDefined(GetType(MyAttribute), False) Then
Console.WriteLine(ControlChars.Cr + "The attributes for the parameter ""{0}"" are : " & ControlChars.Cr, myParameters(j).ParameterType)
' Display all the attributes of type 'MyAttribute' for a parameter.
Dim k As Integer
For k = 0 To myAttributes.Length - 1
Console.WriteLine("The type of the attribute is : {0}", myAttributes(k))
Next k
End If
Next j
Next i
End Sub 'Main
End Class 'MemberInfo_GetCustomAttributes
[C#]
// The following example shows how the attributes related to the parameters of methods
// of the class can be retrieved at run time. A custom parameter level attribute
// named 'MyAttribute' is defined. This custom attribute is associated with a
// parameter of the method 'MyMethod' of 'MyClass1'. This attribute is then
// retrieved at run time for the parameter of the method of the class 'MyClass1'
// and displayed.
using System;
using System.Reflection;
// Define a custom attribute with one named parameter.
[AttributeUsage(AttributeTargets.Parameter)]
public class MyAttribute : Attribute
{
private string myName;
public MyAttribute(string name)
{
myName = name;
}
public string Name
{
get
{
return myName;
}
}
}
// Define a class which has a custom attribute associated with one of parameters of a method.
public class MyClass1
{
public void MyMethod(
[MyAttribute("This is an example parameter attribute")]
int i)
{
return;
}
}
public class MemberInfo_GetCustomAttributes
{
public static void Main()
{
// Get the type of the class 'MyClass1'.
Type myType = typeof(MyClass1);
// Get the members associated with the class 'MyClass1'.
MethodInfo[] myMethods = myType.GetMethods();
// Display the attributes of type 'MyAttribute' for each of the parameters of each method of the class 'MyClass1'.
for(int i = 0; i < myMethods.Length; i++)
{
// Get the parameters for the method.
ParameterInfo[] myParameters = myMethods[i].GetParameters();
Console.WriteLine("\nThe parameters for the method \"{0}\" which have an custom attribute are : \n", myMethods[i]);
for(int j = 0; j < myParameters.Length; j++)
{
// Get the attributes of type 'MyAttribute' for each parameter.
Object[] myAttributes = myParameters[j].GetCustomAttributes(typeof(MyAttribute), false);
if(myParameters[j].IsDefined(typeof(MyAttribute), false))
{
Console.WriteLine("\nThe attributes for the parameter \"{0}\" are : \n", myParameters[j].ParameterType);
// Display all the attributes of type 'MyAttribute' for a parameter.
for(int k = 0; k < myAttributes.Length; k++)
Console.WriteLine("The type of the attribute is : {0}", myAttributes[k]);
}
}
}
}
}
[C++]
// System::Reflection::ParameterInfo::GetCustomAttributes(Type, bool)
// System::Reflection::ParameterInfo::IsDefined(Type, bool)
/*
This example shows how the attributes related to the parameters of methods
of the class can be retrieved at run time. A custom parameter level attribute
named 'MyAttribute' is defined. This custom attribute is associated with a
parameter of the method 'MyMethod' of 'MyClass1'. This attribute is then
retrieved at run time for the parameter of the method of the class 'MyClass1'
and displayed.
*/
#using <mscorlib.dll>
using namespace System;
using namespace System::Reflection;
// Define a custom attribute with one named parameter.
[AttributeUsage(AttributeTargets::Parameter)]
public __gc class MyAttribute : public Attribute
{
private:
String* myName;
public:
MyAttribute(String* name)
{
myName = name;
}
__property String* get_Name()
{
return myName;
}
};
// Define a class which has a custom attribute associated with one of parameters of a method.
public __gc class MyClass1
{
public:
void MyMethod([MyAttribute(S"This is an example parameter attribute")]int i)
{
}
};
void main()
{
// Get the type of the class 'MyClass1'.
Type* myType = __typeof(MyClass1);
// Get the members associated with the class 'MyClass1'.
MethodInfo* myMethods[] = myType->GetMethods();
// Display the attributes of type 'MyAttribute' for each of the parameters of each method of the class 'MyClass1'.
for (int i = 0; i < myMethods->Length; i++) {
// Get the parameters for the method.
ParameterInfo* myParameters[] = myMethods[i]->GetParameters();
Console::WriteLine(S"\nThe parameters for the method \"{0}\" which have an custom attribute are : \n", myMethods[i]);
for (int j = 0; j < myParameters->Length; j++) {
// Get the attributes of type 'MyAttribute' for each parameter.
Object* myAttributes[] = myParameters[j]->GetCustomAttributes(__typeof(MyAttribute), false);
if (myParameters[j]->IsDefined(__typeof(MyAttribute), false)) {
Console::WriteLine(S"\nThe attributes for the parameter \"{0}\" are : \n", myParameters[j]->ParameterType);
// Display all the attributes of type 'MyAttribute' for a parameter.
for (int k = 0; k < myAttributes->Length; k++)
Console::WriteLine(S"The type of the attribute is : {0}", myAttributes[k]);
}
}
}
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET
参照
ParameterInfo クラス | ParameterInfo メンバ | System.Reflection 名前空間