MemberInfo.GetCustomAttributes 方法

定义

在派生类中重写时,返回应用于此成员的自定义属性。

重载

GetCustomAttributes(Boolean)

在派生类中重写时,返回应用于此成员的所有自定义属性的数组。

GetCustomAttributes(Type, Boolean)

在派生类中重写时,返回应用于此成员并由 Type 标识的自定义属性的数组。

GetCustomAttributes(Boolean)

Source:
MemberInfo.cs
Source:
MemberInfo.cs
Source:
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)

Source:
MemberInfo.cs
Source:
MemberInfo.cs
Source:
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,以确定 或 属性是否已CLSCompliantAttributeThreadStaticAttribute应用于它们。 由于 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