MemberInfo.IsDefined(Type, Boolean) 方法

定義

在衍生類別中覆寫時,表示是否已有一個或多個具有指定型別或其衍生型別的屬性套用至這個成員。

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

要搜尋的自訂屬性型別。 此搜尋包括衍生型別。

inherit
Boolean

true 表示要搜尋這個成員的繼承鏈結以尋找屬性;否則為 false。 這個參數會忽略屬性和事件。

傳回

如果已有 attributeType 或其任何衍生型別的一個或多個執行個體套用至這個成員,則為 true,否則為 false

實作

範例

下列範例會判斷指定的屬性是否套用至指定的成員。

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 忽略屬性和事件的 參數。 若要搜尋屬性和事件的繼承鏈結,請使用 方法的適當多載 Attribute.IsDefined

注意

在 .NET Framework 2.0 版中,如果類型、方法或建構函式具有以新元資料格式儲存的安全性屬性,這個方法會傳回 true 。 使用 2.0 版編譯的元件會使用此格式。 使用舊版.NET Framework編譯的動態元件和元件會使用舊的 XML 格式。 請參閱 發出宣告式安全性屬性

適用於