MemberInfo.GetCustomAttributes 方法

定義

在衍生類別中覆寫時,傳回套用至此成員的自訂屬性。

多載

GetCustomAttributes(Boolean)

在衍生類別中覆寫時,傳回套用至此成員之所有自訂屬性的陣列。

GetCustomAttributes(Type, Boolean)

當在衍生的類別中覆寫時,會傳回套用至這個成員的自訂屬性陣列,並以 Type 識別。

GetCustomAttributes(Boolean)

來源:
MemberInfo.cs
來源:
MemberInfo.cs
來源:
MemberInfo.cs

在衍生類別中覆寫時,傳回套用至此成員之所有自訂屬性的陣列。

C#
public abstract object[] GetCustomAttributes(bool inherit);

參數

inherit
Boolean

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

傳回

Object[]

包含套用至此成員之所有自訂屬性的陣列,如果沒有定義屬性,則為包含零個元素的陣列。

實作

例外狀況

這個成員所屬的型別已載入僅限反映的內容中。 請參閱如何:將組件載入僅限反映的內容

無法載入自訂屬性型別。

範例

下列範例會定義自定義屬性,並將 屬性與 MyClass.MyMethod產生關聯,並在運行時間擷取屬性,並顯示結果。

C#
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);
        }
    }
}

備註

這個方法會 inherit 忽略屬性和事件的 參數。 若要搜尋屬性和事件的屬性繼承鏈結,請使用 方法的適當 Attribute.GetCustomAttributes 多載。

備註

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

另請參閱

適用於

.NET 10 和其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetCustomAttributes(Type, Boolean)

來源:
MemberInfo.cs
來源:
MemberInfo.cs
來源:
MemberInfo.cs

當在衍生的類別中覆寫時,會傳回套用至這個成員的自訂屬性陣列,並以 Type 識別。

C#
public abstract object[] GetCustomAttributes(Type attributeType, bool inherit);

參數

attributeType
Type

要搜尋的屬性類型。 只會傳回可指派給這種類型的屬性。

inherit
Boolean

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

傳回

Object[]

套用至這個成員的自訂屬性陣列,或如果 attributeType 沒有套用任何可指派的屬性,則為零個項目的陣列。

實作

例外狀況

無法載入自訂屬性型別。

如果 attributeTypenull

這個成員所屬的型別已載入僅限反映的內容中。 請參閱如何:將組件載入僅限反映的內容

範例

下列範例會定義名為 BaseClass 且具有兩個非繼承成員的類別:名為 total 的線程靜態欄位,以及名為 MethodA的非CLS相容方法。 名為 DerivedClass 的類別繼承自 BaseClass ,並覆寫其 MethodA 方法。 請注意,不會將任何屬性套用至 的成員 DerivedClass。 此範例會逐一查看 的成員DerivedClass,以判斷 或 ThreadStaticAttribute 屬性是否CLSCompliantAttribute套用至它們。 因為 inherittrue,所以 方法會搜尋的繼承階層 DerivedClass 中是否有指定的屬性。 如範例的輸出所示, total 字段會以 ThreadStaticAttribute 屬性裝飾,而 MethodA 方法會以 CLSCompliantAttribute 屬性裝飾。

C#
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

備註

這個方法會 inherit 忽略屬性和事件的 參數。 若要搜尋屬性和事件的屬性繼承鏈結,請使用 方法的適當 Attribute.GetCustomAttributes 多載。

備註

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

適用於

.NET 10 和其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1