MemberInfo.IsDefined(Type, Boolean) MemberInfo.IsDefined(Type, Boolean) MemberInfo.IsDefined(Type, Boolean) MemberInfo.IsDefined(Type, Boolean) Method

定義

派生クラスでオーバーライドされた場合、このメンバーに、指定された型の属性またはその派生型の属性が 1 つ以上適用されているかどうかを示します。When overridden in a derived class, indicates whether one or more attributes of the specified type or of its derived types is applied to this member.

public:
 abstract bool IsDefined(Type ^ attributeType, bool inherit);
public abstract bool IsDefined (Type attributeType, bool inherit);
abstract member IsDefined : Type * bool -> bool
Public MustOverride Function IsDefined (attributeType As Type, inherit As Boolean) As Boolean

パラメーター

attributeType
Type Type Type Type

検索対象のカスタム属性の型。The type of custom attribute to search for. 検索には、派生型が含まれます。The search includes derived types.

inherit
Boolean Boolean Boolean Boolean

このメンバーの継承チェーンを検索して属性を見つける場合は true。それ以外の場合は falsetrue to search this member's inheritance chain to find the attributes; otherwise, false. プロパティとイベントの場合、このパラメーターは無視されます。This parameter is ignored for properties and events.

戻り値

このメンバーに、attributeType の 1 つ以上のインスタンスまたはその派生型が適用されている場合は true。それ以外の場合は falsetrue if one or more instances of attributeType or any of its derived types is applied to this member; otherwise, false.

実装

次の例では、指定された属性が指定されたメンバーに適用されているかどうかを判断します。The following example determines whether the specified attribute is applied to the specified member.

using namespace System;
using namespace System::Reflection;

// Define a custom attribute with one named parameter.

[AttributeUsage(AttributeTargets::All)]
public ref class MyAttribute: public Attribute
{
private:
   String^ myName;

public:
   MyAttribute( String^ name )
   {
      myName = name;
   }

   property String^ Name 
   {
      String^ get()
      {
         return myName;
      }
   }
};

// Define a class that has the custom attribute associated with one of its members.
public ref class MyClass1
{
public:

   [MyAttribute("This is an example attribute.")]
   void MyMethod( int i ){}
};

int main()
{
   try
   {
      // Get the type of MyClass1.
      Type^ myType = MyClass1::typeid;

      // Get the members associated with MyClass1.
      array<MemberInfo^>^myMembers = myType->GetMembers();

      // Display the attributes for each of the members of MyClass1.
      for ( int i = 0; i < myMembers->Length; i++ )
      {
         // Display the attribute if it is of type MyAttribute.
         if ( myMembers[ i ]->IsDefined( MyAttribute::typeid, false ) )
         {
            array<Object^>^myAttributes = myMembers[ i ]->GetCustomAttributes( MyAttribute::typeid, false );
            Console::WriteLine( "\nThe attributes of type MyAttribute for the member {0} are: \n", myMembers[ i ] );
            for ( int j = 0; j < myAttributes->Length; j++ )

               // Display the value associated with the attribute.
               Console::WriteLine( "The value of the attribute is : \"{0}\"",
                        (safe_cast<MyAttribute^>(myAttributes[ j ]))->Name );
         }
      }
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "An exception occurred: {0}", e->Message );
   }
}

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_IsDefined
{
    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++)
            {
                // Display the attribute if it is of type MyAttribute.
                if(myMembers[i].IsDefined(typeof(MyAttribute), false))
                {
                    Object[] myAttributes = myMembers[i].GetCustomAttributes(typeof(MyAttribute), false);
                    Console.WriteLine("\nThe attributes of type MyAttribute for the member {0} are: \n",
                        myMembers[i]);
                    for(int j = 0; j < myAttributes.Length; j++)
                        // Display the value associated with the attribute.
                        Console.WriteLine("The value of the attribute is : \"{0}\"",
                            ((MyAttribute)myAttributes[j]).Name);
                }
            }
        }
        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_IsDefined

    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
                ' Display the attribute if it is of type MyAttribute.
                If myMembers(i).IsDefined(GetType(MyAttribute), False) Then
                    Dim myAttributes As [Object]() = myMembers(i).GetCustomAttributes(GetType(MyAttribute), False)
                    Console.WriteLine(ControlChars.Cr + "The attributes of type MyAttribute for the member {0} are: " + ControlChars.Cr, myMembers(i))
                    Dim j As Integer
                    For j = 0 To myAttributes.Length - 1
                        ' Display the value associated with the attribute.
                        Console.WriteLine("The value of the attribute is : ""{0}""", CType(myAttributes(j), MyAttribute).Name)
                    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プロパティとイベントのパラメーターを無視します。This method ignores the inherit parameter for properties and events. 継承チェーンでプロパティとイベントの属性を検索するには、 Attribute.IsDefinedメソッドの適切なオーバーロードを使用します。To search the inheritance chain for attributes on properties and events, use the appropriate overloads of the Attribute.IsDefined method.

注意

.NET Framework バージョン2.0 では、型、メソッドtrue 、またはコンストラクターが新しいメタデータ形式で格納されているセキュリティ属性を持っている場合、このメソッドはを返します。In the .NET Framework version 2.0, this method returns true if a type, method, or constructor has security attributes stored in the new metadata format. バージョン2.0 でコンパイルされたアセンブリは、この形式を使用します。Assemblies compiled with version 2.0 use this format. 以前のバージョンの .NET Framework でコンパイルされた動的アセンブリとアセンブリは、古い XML 形式を使用します。Dynamic assemblies and assemblies compiled with earlier versions of the .NET Framework use the old XML format. 宣言型セキュリティ属性の出力」を参照してください。See Emitting Declarative Security Attributes.

適用対象