Attribute.GetCustomAttributes 方法

定义

检索应用于程序集、模块、类型成员或方法参数的自定义属性的数组。

重载

GetCustomAttributes(ParameterInfo, Boolean)

检索应用于方法参数的自定义特性的数组。 参数指定方法参数,以及是否搜索方法参数的上级。

GetCustomAttributes(MemberInfo, Type, Boolean)

检索应用于类型成员的自定义属性的数组。 参数指定要搜索的成员、要搜索的自定义属性的类型以及是否搜索成员的上级。

GetCustomAttributes(ParameterInfo, Type, Boolean)

检索应用于方法参数的自定义特性的数组。 参数指定要搜索的方法参数、要搜索的自定义属性的类型,以及是否搜索方法参数的上级。

GetCustomAttributes(Module, Type, Boolean)

检索应用于模块的自定义属性的数组。 参数指定要搜索的模块、要搜索的自定义属性的类型和忽略的搜索选项。

GetCustomAttributes(MemberInfo, Type)

检索应用于类型成员的自定义属性的数组。 参数指定要搜索的成员和自定义属性的类型。

GetCustomAttributes(Assembly, Type, Boolean)

检索应用于程序集的自定义特性的数组。 参数指定要搜索的程序集、要搜索的自定义属性的类型和忽略的搜索选项。

GetCustomAttributes(Module, Type)

检索应用于模块的自定义属性的数组。 参数指定要搜索的模块和自定义属性的类型。

GetCustomAttributes(ParameterInfo, Type)

检索应用于方法参数的自定义特性的数组。 参数指定要搜索的方法参数和自定义属性的类型。

GetCustomAttributes(MemberInfo, Boolean)

检索应用于类型成员的自定义属性的数组。 参数指定要搜索的成员、要搜索的自定义属性的类型以及是否搜索成员的上级。

GetCustomAttributes(Assembly, Type)

检索应用于程序集的自定义特性的数组。 参数指定要搜索的程序集和自定义属性的类型。

GetCustomAttributes(Assembly, Boolean)

检索应用于程序集的自定义特性的数组。 参数指定程序集和忽略的搜索选项。

GetCustomAttributes(ParameterInfo)

检索应用于方法参数的自定义特性的数组。 参数指定方法参数。

GetCustomAttributes(Module)

检索应用于模块的自定义属性的数组。 参数指定模块。

GetCustomAttributes(MemberInfo)

检索应用于类型成员的自定义属性的数组。 参数指定成员。

GetCustomAttributes(Assembly)

检索应用于程序集的自定义特性的数组。 参数指定程序集。

GetCustomAttributes(Module, Boolean)

检索应用于模块的自定义属性的数组。 参数指定模块和忽略的搜索选项。

GetCustomAttributes(ParameterInfo, Boolean)

Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs

检索应用于方法参数的自定义特性的数组。 参数指定方法参数,以及是否搜索方法参数的上级。

C#
public static Attribute[] GetCustomAttributes (System.Reflection.ParameterInfo element, bool inherit);

参数

element
ParameterInfo

一个派生自 ParameterInfo 类的对象,该类描述类的成员的参数。

inherit
Boolean

如果 true,则指定还搜索 element 的上级以获取自定义属性。

返回

一个 Attribute 数组,其中包含应用于 element的自定义属性;如果没有此类自定义属性,则为空数组。

例外

elementMember 属性 null

element null

无法加载自定义属性类型。

示例

下面的代码示例演示如何使用 GetCustomAttributes,以 ParameterInfo 作为参数。

C#
using System;
using System.Reflection;
using System.ComponentModel;

namespace CustAttrs5CS {
    public class AClass {
        public void ParamArrayAndDesc(
            // Add ParamArray (with the keyword) and Description attributes.
            [Description("This argument is a ParamArray")]
            params int[] args)
        {}
    }

    class DemoClass {
        static void Main(string[] args) {
            // Get the Class type to access its metadata.
            Type clsType = typeof(AClass);
            // Get the type information for the method.
            MethodInfo mInfo = clsType.GetMethod("ParamArrayAndDesc");
            if (mInfo != null) {
                // Get the parameter information.
                ParameterInfo[] pInfo = mInfo.GetParameters();
                if (pInfo != null) {
                    // Iterate through all the attributes for the parameter.
                    foreach(Attribute attr in
                        Attribute.GetCustomAttributes(pInfo[0])) {
                        // Check for the ParamArray attribute.
                        if (attr.GetType() == typeof(ParamArrayAttribute))
                            Console.WriteLine("Parameter {0} for method {1} " +
                                "has the ParamArray attribute.",
                                pInfo[0].Name, mInfo.Name);
                        // Check for the Description attribute.
                        else if (attr.GetType() ==
                            typeof(DescriptionAttribute)) {
                            Console.WriteLine("Parameter {0} for method {1} " +
                                "has a description attribute.",
                                pInfo[0].Name, mInfo.Name);
                            Console.WriteLine("The description is: \"{0}\"",
                                ((DescriptionAttribute)attr).Description);
                        }
                    }
                }
            }
        }
    }
}

/*
 * Output:
 * Parameter args for method ParamArrayAndDesc has the ParamArray attribute.
 * Parameter args for method ParamArrayAndDesc has a description attribute.
 * The description is: "This argument is a ParamArray"
 */

注解

如果 element 表示派生类型方法中的参数,则返回值包括应用于重写基方法中相同参数的可继承自定义属性。

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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(MemberInfo, Type, Boolean)

Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs

检索应用于类型成员的自定义属性的数组。 参数指定要搜索的成员、要搜索的自定义属性的类型以及是否搜索成员的上级。

C#
public static Attribute[] GetCustomAttributes (System.Reflection.MemberInfo element, Type type, bool inherit);
C#
public static Attribute[] GetCustomAttributes (System.Reflection.MemberInfo element, Type attributeType, bool inherit);

参数

element
MemberInfo

一个派生自 MemberInfo 类的对象,该类描述类的构造函数、事件、字段、方法或属性成员。

typeattributeType
Type

要搜索的自定义属性的类型或基类型。

inherit
Boolean

如果 true,则指定还搜索 element 的上级以获取自定义属性。

返回

一个 Attribute 数组,其中包含应用于 elementtype 类型的自定义属性;如果没有此类自定义属性,则为空数组。

例外

elementtypenull

type 不派生自 Attribute

element 不是构造函数、方法、属性、事件、类型或字段。

无法加载自定义属性类型。

示例

下面的代码示例演示如何使用 GetCustomAttributes,以 MemberInfo 作为参数。

C#
using System;
using System.Reflection;
using System.Security;
using System.Runtime.InteropServices;

namespace CustAttrs4CS
{

    // Define an enumeration of Win32 unmanaged types
    public enum UnmanagedType
    {
        User,
        GDI,
        Kernel,
        Shell,
        Networking,
        Multimedia
    }

    // Define the Unmanaged attribute.
    public class UnmanagedAttribute : Attribute
    {
        // Storage for the UnmanagedType value.
        protected UnmanagedType thisType;

        // Set the unmanaged type in the constructor.
        public UnmanagedAttribute(UnmanagedType type)
        {
            thisType = type;
        }

        // Define a property to get and set the UnmanagedType value.
        public UnmanagedType Win32Type
        {
            get { return thisType; }
            set { thisType = Win32Type; }
        }
    }

    // Create a class for an imported Win32 unmanaged function.
    public class Win32 {
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        public static extern int MessageBox(int hWnd, String text,
            String caption, uint type);
    }

    public class AClass {
        // Add some attributes to Win32CallMethod.
        [Obsolete("This method is obsolete. Use managed MsgBox instead.")]
        [Unmanaged(UnmanagedType.User)]
        public void Win32CallMethod()
        {
            Win32.MessageBox(0, "This is an unmanaged call.", "Caution!", 0);
        }
    }

    class DemoClass {
        static void Main(string[] args)
            {
            // Get the AClass type to access its metadata.
            Type clsType = typeof(AClass);
            // Get the type information for Win32CallMethod.
            MethodInfo mInfo = clsType.GetMethod("Win32CallMethod");
            if (mInfo != null)
            {
                // Iterate through all the attributes of the method.
                foreach(Attribute attr in
                    Attribute.GetCustomAttributes(mInfo)) {
                    // Check for the Obsolete attribute.
                    if (attr.GetType() == typeof(ObsoleteAttribute))
                    {
                        Console.WriteLine("Method {0} is obsolete. " +
                            "The message is:",
                            mInfo.Name);
                        Console.WriteLine("  \"{0}\"",
                            ((ObsoleteAttribute)attr).Message);
                    }

                    // Check for the Unmanaged attribute.
                    else if (attr.GetType() == typeof(UnmanagedAttribute))
                    {
                        Console.WriteLine(
                            "This method calls unmanaged code.");
                        Console.WriteLine(
                            String.Format("The Unmanaged attribute type is {0}.",
                                          ((UnmanagedAttribute)attr).Win32Type));
                        AClass myCls = new AClass();
                        myCls.Win32CallMethod();
                    }
                }
            }
        }
    }
}

/*

This code example produces the following results.

First, the compilation yields the warning, "... This method is
obsolete. Use managed MsgBox instead."
Second, execution yields a message box with a title of "Caution!"
and message text of "This is an unmanaged call."
Third, the following text is displayed in the console window:

Method Win32CallMethod is obsolete. The message is:
  "This method is obsolete. Use managed MsgBox instead."
This method calls unmanaged code.
The Unmanaged attribute type is User.

*/

注解

如果 inherittrue,则返回值包含 element 的上级自定义属性。

备注

从 .NET Framework 版本 2.0 开始,如果属性以新的元数据格式存储,此方法将返回方法、构造函数和类型的安全属性。 使用版本 2.0 或更高版本编译的程序集使用新格式。 使用早期版本的 .NET Framework 编译的动态程序集和程序集使用旧的 XML 格式。 请参阅 发出声明性安全属性。

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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(ParameterInfo, Type, Boolean)

Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs

检索应用于方法参数的自定义特性的数组。 参数指定要搜索的方法参数、要搜索的自定义属性的类型,以及是否搜索方法参数的上级。

C#
public static Attribute[] GetCustomAttributes (System.Reflection.ParameterInfo element, Type attributeType, bool inherit);

参数

element
ParameterInfo

一个派生自 ParameterInfo 类的对象,该类描述类的成员的参数。

attributeType
Type

要搜索的自定义属性的类型或基类型。

inherit
Boolean

如果 true,则指定还搜索 element 的上级以获取自定义属性。

返回

一个 Attribute 数组,其中包含应用于 elementattributeType 类型的自定义属性;如果没有此类自定义属性,则为空数组。

例外

elementattributeTypenull

attributeType 不派生自 Attribute

无法加载自定义属性类型。

示例

下面的代码示例演示如何使用 GetCustomAttributes,以 ParameterInfo 作为参数。

C#
using System;
using System.Reflection;
using System.ComponentModel;

namespace CustAttrs5CS {
    public class AClass {
        public void ParamArrayAndDesc(
            // Add ParamArray (with the keyword) and Description attributes.
            [Description("This argument is a ParamArray")]
            params int[] args)
        {}
    }

    class DemoClass {
        static void Main(string[] args) {
            // Get the Class type to access its metadata.
            Type clsType = typeof(AClass);
            // Get the type information for the method.
            MethodInfo mInfo = clsType.GetMethod("ParamArrayAndDesc");
            if (mInfo != null) {
                // Get the parameter information.
                ParameterInfo[] pInfo = mInfo.GetParameters();
                if (pInfo != null) {
                    // Iterate through all the attributes for the parameter.
                    foreach(Attribute attr in
                        Attribute.GetCustomAttributes(pInfo[0])) {
                        // Check for the ParamArray attribute.
                        if (attr.GetType() == typeof(ParamArrayAttribute))
                            Console.WriteLine("Parameter {0} for method {1} " +
                                "has the ParamArray attribute.",
                                pInfo[0].Name, mInfo.Name);
                        // Check for the Description attribute.
                        else if (attr.GetType() ==
                            typeof(DescriptionAttribute)) {
                            Console.WriteLine("Parameter {0} for method {1} " +
                                "has a description attribute.",
                                pInfo[0].Name, mInfo.Name);
                            Console.WriteLine("The description is: \"{0}\"",
                                ((DescriptionAttribute)attr).Description);
                        }
                    }
                }
            }
        }
    }
}

/*
 * Output:
 * Parameter args for method ParamArrayAndDesc has the ParamArray attribute.
 * Parameter args for method ParamArrayAndDesc has a description attribute.
 * The description is: "This argument is a ParamArray"
 */

注解

如果 element 表示派生类型方法中的参数,则返回值包括应用于重写基方法中相同参数的可继承自定义属性。

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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(Module, Type, Boolean)

Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs

检索应用于模块的自定义属性的数组。 参数指定要搜索的模块、要搜索的自定义属性的类型和忽略的搜索选项。

C#
public static Attribute[] GetCustomAttributes (System.Reflection.Module element, Type attributeType, bool inherit);

参数

element
Module

一个派生自描述可移植可执行文件的 Module 类的对象。

attributeType
Type

要搜索的自定义属性的类型或基类型。

inherit
Boolean

忽略此参数,不会影响此方法的操作。

返回

一个 Attribute 数组,其中包含应用于 elementattributeType 类型的自定义属性;如果没有此类自定义属性,则为空数组。

例外

elementattributeTypenull

attributeType 不派生自 Attribute

示例

下面的代码示例演示如何使用 GetCustomAttributes,以 Module 作为参数。

C#
using System;
using System.Reflection;
using System.ComponentModel;

// Assign some attributes to the module.
[module:Description("A sample description")]

// Set the module's CLSCompliant attribute to false
// The CLSCompliant attribute is applicable for /target:module.
[module:CLSCompliant(false)]

namespace CustAttrs2CS {
    class DemoClass {
        static void Main(string[] args) {
            Type clsType = typeof(DemoClass);
            // Get the Module type to access its metadata.
            Module module = clsType.Module;

            // Iterate through all the attributes for the module.
            foreach(Attribute attr in Attribute.GetCustomAttributes(module)) {
                // Check for the Description attribute.
                if (attr.GetType() == typeof(DescriptionAttribute))
                    Console.WriteLine("Module {0} has the description " +
                        "\"{1}\".", module.Name,
                        ((DescriptionAttribute)attr).Description);
                // Check for the CLSCompliant attribute.
                else if (attr.GetType() == typeof(CLSCompliantAttribute))
                    Console.WriteLine("Module {0} {1} CLSCompliant.",
                        module.Name,
                        ((CLSCompliantAttribute)attr).IsCompliant ?
                            "is" : "is not");
            }
        }
    }
}

/*
 * Output:
 * Module CustAttrs2CS.exe is not CLSCompliant.
 * Module CustAttrs2CS.exe has the description "A sample description".
 */

注解

如果 inherittrue,则返回值包含 element 的上级自定义属性。

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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(MemberInfo, Type)

Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs

检索应用于类型成员的自定义属性的数组。 参数指定要搜索的成员和自定义属性的类型。

C#
public static Attribute[] GetCustomAttributes (System.Reflection.MemberInfo element, Type type);
C#
public static Attribute[] GetCustomAttributes (System.Reflection.MemberInfo element, Type attributeType);

参数

element
MemberInfo

一个派生自 MemberInfo 类的对象,该类描述类的构造函数、事件、字段、方法或属性成员。

typeattributeType
Type

要搜索的自定义属性的类型或基类型。

返回

一个 Attribute 数组,其中包含应用于 elementtype 类型的自定义属性;如果没有此类自定义属性,则为空数组。

例外

elementtypenull

type 不派生自 Attribute

element 不是构造函数、方法、属性、事件、类型或字段。

无法加载自定义属性类型。

示例

下面的代码示例演示如何使用 GetCustomAttribute,以 MemberInfo 作为参数。

C#
using System;
using System.Reflection;
using System.Security;
using System.Runtime.InteropServices;

namespace CustAttrs4CS
{

    // Define an enumeration of Win32 unmanaged types
    public enum UnmanagedType
    {
        User,
        GDI,
        Kernel,
        Shell,
        Networking,
        Multimedia
    }

    // Define the Unmanaged attribute.
    public class UnmanagedAttribute : Attribute
    {
        // Storage for the UnmanagedType value.
        protected UnmanagedType thisType;

        // Set the unmanaged type in the constructor.
        public UnmanagedAttribute(UnmanagedType type)
        {
            thisType = type;
        }

        // Define a property to get and set the UnmanagedType value.
        public UnmanagedType Win32Type
        {
            get { return thisType; }
            set { thisType = Win32Type; }
        }
    }

    // Create a class for an imported Win32 unmanaged function.
    public class Win32 {
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        public static extern int MessageBox(int hWnd, String text,
            String caption, uint type);
    }

    public class AClass {
        // Add some attributes to Win32CallMethod.
        [Obsolete("This method is obsolete. Use managed MsgBox instead.")]
        [Unmanaged(UnmanagedType.User)]
        public void Win32CallMethod()
        {
            Win32.MessageBox(0, "This is an unmanaged call.", "Caution!", 0);
        }
    }

    class DemoClass {
        static void Main(string[] args)
            {
            // Get the AClass type to access its metadata.
            Type clsType = typeof(AClass);
            // Get the type information for Win32CallMethod.
            MethodInfo mInfo = clsType.GetMethod("Win32CallMethod");
            if (mInfo != null)
            {
                // Iterate through all the attributes of the method.
                foreach(Attribute attr in
                    Attribute.GetCustomAttributes(mInfo)) {
                    // Check for the Obsolete attribute.
                    if (attr.GetType() == typeof(ObsoleteAttribute))
                    {
                        Console.WriteLine("Method {0} is obsolete. " +
                            "The message is:",
                            mInfo.Name);
                        Console.WriteLine("  \"{0}\"",
                            ((ObsoleteAttribute)attr).Message);
                    }

                    // Check for the Unmanaged attribute.
                    else if (attr.GetType() == typeof(UnmanagedAttribute))
                    {
                        Console.WriteLine(
                            "This method calls unmanaged code.");
                        Console.WriteLine(
                            String.Format("The Unmanaged attribute type is {0}.",
                                          ((UnmanagedAttribute)attr).Win32Type));
                        AClass myCls = new AClass();
                        myCls.Win32CallMethod();
                    }
                }
            }
        }
    }
}

/*

This code example produces the following results.

First, the compilation yields the warning, "... This method is
obsolete. Use managed MsgBox instead."
Second, execution yields a message box with a title of "Caution!"
and message text of "This is an unmanaged call."
Third, the following text is displayed in the console window:

Method Win32CallMethod is obsolete. The message is:
  "This method is obsolete. Use managed MsgBox instead."
This method calls unmanaged code.
The Unmanaged attribute type is User.

*/

注解

返回值包含 element上级自定义属性。

备注

从 .NET Framework 版本 2.0 开始,如果属性以新的元数据格式存储,此方法将返回方法、构造函数和类型的安全属性。 使用版本 2.0 或更高版本编译的程序集使用新格式。 使用早期版本的 .NET Framework 编译的动态程序集和程序集使用旧的 XML 格式。 请参阅 发出声明性安全属性。

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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(Assembly, Type, Boolean)

Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs

检索应用于程序集的自定义特性的数组。 参数指定要搜索的程序集、要搜索的自定义属性的类型和忽略的搜索选项。

C#
public static Attribute[] GetCustomAttributes (System.Reflection.Assembly element, Type attributeType, bool inherit);

参数

element
Assembly

一个从 Assembly 类派生的对象,该类描述模块的可重用集合。

attributeType
Type

要搜索的自定义属性的类型或基类型。

inherit
Boolean

忽略此参数,不会影响此方法的操作。

返回

一个 Attribute 数组,其中包含应用于 elementattributeType 类型的自定义属性;如果没有此类自定义属性,则为空数组。

例外

elementattributeTypenull

attributeType 不派生自 Attribute

示例

下面的代码示例演示如何使用 GetCustomAttributes,以 Assembly 作为参数。

C#
using System;
using System.Reflection;

[assembly: AssemblyTitle("CustAttrs1CS")]
[assembly: AssemblyDescription("GetCustomAttributes() Demo")]
[assembly: AssemblyCompany("Microsoft")]

class Example {
    static void Main() {
        // Get the Assembly object to access its metadata.
        Assembly assy = typeof(Example).Assembly;

        // Iterate through the attributes for the assembly.
        foreach(Attribute attr in Attribute.GetCustomAttributes(assy)) {
            // Check for the AssemblyTitle attribute.
            if (attr.GetType() == typeof(AssemblyTitleAttribute))
                Console.WriteLine("Assembly title is \"{0}\".",
                    ((AssemblyTitleAttribute)attr).Title);

            // Check for the AssemblyDescription attribute.
            else if (attr.GetType() ==
                typeof(AssemblyDescriptionAttribute))
                Console.WriteLine("Assembly description is \"{0}\".",
                    ((AssemblyDescriptionAttribute)attr).Description);

            // Check for the AssemblyCompany attribute.
            else if (attr.GetType() == typeof(AssemblyCompanyAttribute))
                Console.WriteLine("Assembly company is {0}.",
                    ((AssemblyCompanyAttribute)attr).Company);
        }
   }
}
// The example displays the following output:
//     Assembly title is "CustAttrs1CS".
//     Assembly description is "GetCustomAttributes() Demo".
//     Assembly company is Microsoft.

注解

备注

从 .NET Framework 版本 2.0 开始,如果属性以新的元数据格式存储,此方法将返回安全属性。 使用版本 2.0 或更高版本编译的程序集使用新格式。 使用早期版本的 .NET Framework 编译的动态程序集和程序集使用旧的 XML 格式。 请参阅 发出声明性安全属性。

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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(Module, Type)

Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs

检索应用于模块的自定义属性的数组。 参数指定要搜索的模块和自定义属性的类型。

C#
public static Attribute[] GetCustomAttributes (System.Reflection.Module element, Type attributeType);

参数

element
Module

一个派生自描述可移植可执行文件的 Module 类的对象。

attributeType
Type

要搜索的自定义属性的类型或基类型。

返回

一个 Attribute 数组,其中包含应用于 elementattributeType 类型的自定义属性;如果没有此类自定义属性,则为空数组。

例外

elementattributeTypenull

attributeType 不派生自 Attribute

示例

下面的代码示例演示如何使用 GetCustomAttributes,以 Module 作为参数。

C#
using System;
using System.Reflection;
using System.ComponentModel;

// Assign some attributes to the module.
[module:Description("A sample description")]

// Set the module's CLSCompliant attribute to false
// The CLSCompliant attribute is applicable for /target:module.
[module:CLSCompliant(false)]

namespace CustAttrs2CS {
    class DemoClass {
        static void Main(string[] args) {
            Type clsType = typeof(DemoClass);
            // Get the Module type to access its metadata.
            Module module = clsType.Module;

            // Iterate through all the attributes for the module.
            foreach(Attribute attr in Attribute.GetCustomAttributes(module)) {
                // Check for the Description attribute.
                if (attr.GetType() == typeof(DescriptionAttribute))
                    Console.WriteLine("Module {0} has the description " +
                        "\"{1}\".", module.Name,
                        ((DescriptionAttribute)attr).Description);
                // Check for the CLSCompliant attribute.
                else if (attr.GetType() == typeof(CLSCompliantAttribute))
                    Console.WriteLine("Module {0} {1} CLSCompliant.",
                        module.Name,
                        ((CLSCompliantAttribute)attr).IsCompliant ?
                            "is" : "is not");
            }
        }
    }
}

/*
 * Output:
 * Module CustAttrs2CS.exe is not CLSCompliant.
 * Module CustAttrs2CS.exe has the description "A sample description".
 */

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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(ParameterInfo, Type)

Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs

检索应用于方法参数的自定义特性的数组。 参数指定要搜索的方法参数和自定义属性的类型。

C#
public static Attribute[] GetCustomAttributes (System.Reflection.ParameterInfo element, Type attributeType);

参数

element
ParameterInfo

一个派生自 ParameterInfo 类的对象,该类描述类的成员的参数。

attributeType
Type

要搜索的自定义属性的类型或基类型。

返回

一个 Attribute 数组,其中包含应用于 elementattributeType 类型的自定义属性;如果没有此类自定义属性,则为空数组。

例外

elementattributeTypenull

attributeType 不派生自 Attribute

无法加载自定义属性类型。

示例

下面的代码示例演示如何使用 GetCustomAttributes,以 ParameterInfo 作为参数。

C#
using System;
using System.Reflection;
using System.ComponentModel;

namespace CustAttrs5CS {
    public class AClass {
        public void ParamArrayAndDesc(
            // Add ParamArray (with the keyword) and Description attributes.
            [Description("This argument is a ParamArray")]
            params int[] args)
        {}
    }

    class DemoClass {
        static void Main(string[] args) {
            // Get the Class type to access its metadata.
            Type clsType = typeof(AClass);
            // Get the type information for the method.
            MethodInfo mInfo = clsType.GetMethod("ParamArrayAndDesc");
            if (mInfo != null) {
                // Get the parameter information.
                ParameterInfo[] pInfo = mInfo.GetParameters();
                if (pInfo != null) {
                    // Iterate through all the attributes for the parameter.
                    foreach(Attribute attr in
                        Attribute.GetCustomAttributes(pInfo[0])) {
                        // Check for the ParamArray attribute.
                        if (attr.GetType() == typeof(ParamArrayAttribute))
                            Console.WriteLine("Parameter {0} for method {1} " +
                                "has the ParamArray attribute.",
                                pInfo[0].Name, mInfo.Name);
                        // Check for the Description attribute.
                        else if (attr.GetType() ==
                            typeof(DescriptionAttribute)) {
                            Console.WriteLine("Parameter {0} for method {1} " +
                                "has a description attribute.",
                                pInfo[0].Name, mInfo.Name);
                            Console.WriteLine("The description is: \"{0}\"",
                                ((DescriptionAttribute)attr).Description);
                        }
                    }
                }
            }
        }
    }
}

/*
 * Output:
 * Parameter args for method ParamArrayAndDesc has the ParamArray attribute.
 * Parameter args for method ParamArrayAndDesc has a description attribute.
 * The description is: "This argument is a ParamArray"
 */

注解

如果 element 表示派生类型方法中的参数,则返回值包括应用于重写基方法中相同参数的可继承自定义属性。

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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(MemberInfo, Boolean)

Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs

检索应用于类型成员的自定义属性的数组。 参数指定要搜索的成员、要搜索的自定义属性的类型以及是否搜索成员的上级。

C#
public static Attribute[] GetCustomAttributes (System.Reflection.MemberInfo element, bool inherit);

参数

element
MemberInfo

一个派生自 MemberInfo 类的对象,该类描述类的构造函数、事件、字段、方法或属性成员。

inherit
Boolean

如果 true,则指定还搜索 element 的上级以获取自定义属性。

返回

一个 Attribute 数组,其中包含应用于 element的自定义属性;如果没有此类自定义属性,则为空数组。

例外

element null

element 不是构造函数、方法、属性、事件、类型或字段。

无法加载自定义属性类型。

示例

下面的代码示例演示如何使用 GetCustomAttributes,以 MemberInfo 作为参数。

C#
using System;
using System.Reflection;
using System.Security;
using System.Runtime.InteropServices;

namespace CustAttrs4CS
{

    // Define an enumeration of Win32 unmanaged types
    public enum UnmanagedType
    {
        User,
        GDI,
        Kernel,
        Shell,
        Networking,
        Multimedia
    }

    // Define the Unmanaged attribute.
    public class UnmanagedAttribute : Attribute
    {
        // Storage for the UnmanagedType value.
        protected UnmanagedType thisType;

        // Set the unmanaged type in the constructor.
        public UnmanagedAttribute(UnmanagedType type)
        {
            thisType = type;
        }

        // Define a property to get and set the UnmanagedType value.
        public UnmanagedType Win32Type
        {
            get { return thisType; }
            set { thisType = Win32Type; }
        }
    }

    // Create a class for an imported Win32 unmanaged function.
    public class Win32 {
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        public static extern int MessageBox(int hWnd, String text,
            String caption, uint type);
    }

    public class AClass {
        // Add some attributes to Win32CallMethod.
        [Obsolete("This method is obsolete. Use managed MsgBox instead.")]
        [Unmanaged(UnmanagedType.User)]
        public void Win32CallMethod()
        {
            Win32.MessageBox(0, "This is an unmanaged call.", "Caution!", 0);
        }
    }

    class DemoClass {
        static void Main(string[] args)
            {
            // Get the AClass type to access its metadata.
            Type clsType = typeof(AClass);
            // Get the type information for Win32CallMethod.
            MethodInfo mInfo = clsType.GetMethod("Win32CallMethod");
            if (mInfo != null)
            {
                // Iterate through all the attributes of the method.
                foreach(Attribute attr in
                    Attribute.GetCustomAttributes(mInfo)) {
                    // Check for the Obsolete attribute.
                    if (attr.GetType() == typeof(ObsoleteAttribute))
                    {
                        Console.WriteLine("Method {0} is obsolete. " +
                            "The message is:",
                            mInfo.Name);
                        Console.WriteLine("  \"{0}\"",
                            ((ObsoleteAttribute)attr).Message);
                    }

                    // Check for the Unmanaged attribute.
                    else if (attr.GetType() == typeof(UnmanagedAttribute))
                    {
                        Console.WriteLine(
                            "This method calls unmanaged code.");
                        Console.WriteLine(
                            String.Format("The Unmanaged attribute type is {0}.",
                                          ((UnmanagedAttribute)attr).Win32Type));
                        AClass myCls = new AClass();
                        myCls.Win32CallMethod();
                    }
                }
            }
        }
    }
}

/*

This code example produces the following results.

First, the compilation yields the warning, "... This method is
obsolete. Use managed MsgBox instead."
Second, execution yields a message box with a title of "Caution!"
and message text of "This is an unmanaged call."
Third, the following text is displayed in the console window:

Method Win32CallMethod is obsolete. The message is:
  "This method is obsolete. Use managed MsgBox instead."
This method calls unmanaged code.
The Unmanaged attribute type is User.

*/

注解

如果 inherittrue,则返回值包含 element 的上级自定义属性。

备注

从 .NET Framework 版本 2.0 开始,如果属性以新的元数据格式存储,此方法将返回方法、构造函数和类型的安全属性。 使用版本 2.0 或更高版本编译的程序集使用新格式。 使用早期版本的 .NET Framework 编译的动态程序集和程序集使用旧的 XML 格式。 请参阅 发出声明性安全属性。

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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(Assembly, Type)

Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs

检索应用于程序集的自定义特性的数组。 参数指定要搜索的程序集和自定义属性的类型。

C#
public static Attribute[] GetCustomAttributes (System.Reflection.Assembly element, Type attributeType);

参数

element
Assembly

一个从 Assembly 类派生的对象,该类描述模块的可重用集合。

attributeType
Type

要搜索的自定义属性的类型或基类型。

返回

一个 Attribute 数组,其中包含应用于 elementattributeType 类型的自定义属性;如果没有此类自定义属性,则为空数组。

例外

elementattributeTypenull

attributeType 不派生自 Attribute

示例

下面的代码示例演示如何使用 GetCustomAttributes,以 Assembly 作为参数。

C#
using System;
using System.Reflection;

[assembly: AssemblyTitle("CustAttrs1CS")]
[assembly: AssemblyDescription("GetCustomAttributes() Demo")]
[assembly: AssemblyCompany("Microsoft")]

class Example {
    static void Main() {
        // Get the Assembly object to access its metadata.
        Assembly assy = typeof(Example).Assembly;

        // Iterate through the attributes for the assembly.
        foreach(Attribute attr in Attribute.GetCustomAttributes(assy)) {
            // Check for the AssemblyTitle attribute.
            if (attr.GetType() == typeof(AssemblyTitleAttribute))
                Console.WriteLine("Assembly title is \"{0}\".",
                    ((AssemblyTitleAttribute)attr).Title);

            // Check for the AssemblyDescription attribute.
            else if (attr.GetType() ==
                typeof(AssemblyDescriptionAttribute))
                Console.WriteLine("Assembly description is \"{0}\".",
                    ((AssemblyDescriptionAttribute)attr).Description);

            // Check for the AssemblyCompany attribute.
            else if (attr.GetType() == typeof(AssemblyCompanyAttribute))
                Console.WriteLine("Assembly company is {0}.",
                    ((AssemblyCompanyAttribute)attr).Company);
        }
   }
}
// The example displays the following output:
//     Assembly title is "CustAttrs1CS".
//     Assembly description is "GetCustomAttributes() Demo".
//     Assembly company is Microsoft.

注解

备注

从 .NET Framework 版本 2.0 开始,如果属性以新的元数据格式存储,此方法将返回安全属性。 使用版本 2.0 或更高版本编译的程序集使用新格式。 使用早期版本的 .NET Framework 编译的动态程序集和程序集使用旧的 XML 格式。 请参阅 发出声明性安全属性。

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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(Assembly, Boolean)

Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs

检索应用于程序集的自定义特性的数组。 参数指定程序集和忽略的搜索选项。

C#
public static Attribute[] GetCustomAttributes (System.Reflection.Assembly element, bool inherit);

参数

element
Assembly

一个从 Assembly 类派生的对象,该类描述模块的可重用集合。

inherit
Boolean

忽略此参数,不会影响此方法的操作。

返回

一个 Attribute 数组,其中包含应用于 element的自定义属性;如果没有此类自定义属性,则为空数组。

例外

element null

示例

下面的代码示例演示如何使用 GetCustomAttributes,以 Assembly 作为参数。

C#
using System;
using System.Reflection;

[assembly: AssemblyTitle("CustAttrs1CS")]
[assembly: AssemblyDescription("GetCustomAttributes() Demo")]
[assembly: AssemblyCompany("Microsoft")]

class Example {
    static void Main() {
        // Get the Assembly object to access its metadata.
        Assembly assy = typeof(Example).Assembly;

        // Iterate through the attributes for the assembly.
        foreach(Attribute attr in Attribute.GetCustomAttributes(assy)) {
            // Check for the AssemblyTitle attribute.
            if (attr.GetType() == typeof(AssemblyTitleAttribute))
                Console.WriteLine("Assembly title is \"{0}\".",
                    ((AssemblyTitleAttribute)attr).Title);

            // Check for the AssemblyDescription attribute.
            else if (attr.GetType() ==
                typeof(AssemblyDescriptionAttribute))
                Console.WriteLine("Assembly description is \"{0}\".",
                    ((AssemblyDescriptionAttribute)attr).Description);

            // Check for the AssemblyCompany attribute.
            else if (attr.GetType() == typeof(AssemblyCompanyAttribute))
                Console.WriteLine("Assembly company is {0}.",
                    ((AssemblyCompanyAttribute)attr).Company);
        }
   }
}
// The example displays the following output:
//     Assembly title is "CustAttrs1CS".
//     Assembly description is "GetCustomAttributes() Demo".
//     Assembly company is Microsoft.

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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(ParameterInfo)

Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs

检索应用于方法参数的自定义特性的数组。 参数指定方法参数。

C#
public static Attribute[] GetCustomAttributes (System.Reflection.ParameterInfo element);

参数

element
ParameterInfo

一个派生自 ParameterInfo 类的对象,该类描述类的成员的参数。

返回

一个 Attribute 数组,其中包含应用于 element的自定义属性;如果没有此类自定义属性,则为空数组。

例外

element null

无法加载自定义属性类型。

示例

下面的代码示例演示如何使用 GetCustomAttributes,以 ParameterInfo 作为参数。

C#
using System;
using System.Reflection;
using System.ComponentModel;

namespace CustAttrs5CS {
    public class AClass {
        public void ParamArrayAndDesc(
            // Add ParamArray (with the keyword) and Description attributes.
            [Description("This argument is a ParamArray")]
            params int[] args)
        {}
    }

    class DemoClass {
        static void Main(string[] args) {
            // Get the Class type to access its metadata.
            Type clsType = typeof(AClass);
            // Get the type information for the method.
            MethodInfo mInfo = clsType.GetMethod("ParamArrayAndDesc");
            if (mInfo != null) {
                // Get the parameter information.
                ParameterInfo[] pInfo = mInfo.GetParameters();
                if (pInfo != null) {
                    // Iterate through all the attributes for the parameter.
                    foreach(Attribute attr in
                        Attribute.GetCustomAttributes(pInfo[0])) {
                        // Check for the ParamArray attribute.
                        if (attr.GetType() == typeof(ParamArrayAttribute))
                            Console.WriteLine("Parameter {0} for method {1} " +
                                "has the ParamArray attribute.",
                                pInfo[0].Name, mInfo.Name);
                        // Check for the Description attribute.
                        else if (attr.GetType() ==
                            typeof(DescriptionAttribute)) {
                            Console.WriteLine("Parameter {0} for method {1} " +
                                "has a description attribute.",
                                pInfo[0].Name, mInfo.Name);
                            Console.WriteLine("The description is: \"{0}\"",
                                ((DescriptionAttribute)attr).Description);
                        }
                    }
                }
            }
        }
    }
}

/*
 * Output:
 * Parameter args for method ParamArrayAndDesc has the ParamArray attribute.
 * Parameter args for method ParamArrayAndDesc has a description attribute.
 * The description is: "This argument is a ParamArray"
 */

注解

如果 element 表示派生类型方法中的参数,则返回值包括应用于重写基方法中相同参数的可继承自定义属性。

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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(Module)

Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs

检索应用于模块的自定义属性的数组。 参数指定模块。

C#
public static Attribute[] GetCustomAttributes (System.Reflection.Module element);

参数

element
Module

一个派生自描述可移植可执行文件的 Module 类的对象。

返回

一个 Attribute 数组,其中包含应用于 element的自定义属性;如果没有此类自定义属性,则为空数组。

例外

element null

示例

下面的代码示例演示如何使用 GetCustomAttributes,以 Module 作为参数。

C#
using System;
using System.Reflection;
using System.ComponentModel;

// Assign some attributes to the module.
[module:Description("A sample description")]

// Set the module's CLSCompliant attribute to false
// The CLSCompliant attribute is applicable for /target:module.
[module:CLSCompliant(false)]

namespace CustAttrs2CS {
    class DemoClass {
        static void Main(string[] args) {
            Type clsType = typeof(DemoClass);
            // Get the Module type to access its metadata.
            Module module = clsType.Module;

            // Iterate through all the attributes for the module.
            foreach(Attribute attr in Attribute.GetCustomAttributes(module)) {
                // Check for the Description attribute.
                if (attr.GetType() == typeof(DescriptionAttribute))
                    Console.WriteLine("Module {0} has the description " +
                        "\"{1}\".", module.Name,
                        ((DescriptionAttribute)attr).Description);
                // Check for the CLSCompliant attribute.
                else if (attr.GetType() == typeof(CLSCompliantAttribute))
                    Console.WriteLine("Module {0} {1} CLSCompliant.",
                        module.Name,
                        ((CLSCompliantAttribute)attr).IsCompliant ?
                            "is" : "is not");
            }
        }
    }
}

/*
 * Output:
 * Module CustAttrs2CS.exe is not CLSCompliant.
 * Module CustAttrs2CS.exe has the description "A sample description".
 */

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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(MemberInfo)

Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs

检索应用于类型成员的自定义属性的数组。 参数指定成员。

C#
public static Attribute[] GetCustomAttributes (System.Reflection.MemberInfo element);

参数

element
MemberInfo

一个派生自 MemberInfo 类的对象,该类描述类的构造函数、事件、字段、方法或属性成员。

返回

一个 Attribute 数组,其中包含应用于 element的自定义属性;如果没有此类自定义属性,则为空数组。

例外

element null

element 不是构造函数、方法、属性、事件、类型或字段。

无法加载自定义属性类型。

示例

下面的代码示例演示如何使用 GetCustomAttribute,以 MemberInfo 作为参数。

C#
using System;
using System.Reflection;
using System.Security;
using System.Runtime.InteropServices;

namespace CustAttrs4CS
{

    // Define an enumeration of Win32 unmanaged types
    public enum UnmanagedType
    {
        User,
        GDI,
        Kernel,
        Shell,
        Networking,
        Multimedia
    }

    // Define the Unmanaged attribute.
    public class UnmanagedAttribute : Attribute
    {
        // Storage for the UnmanagedType value.
        protected UnmanagedType thisType;

        // Set the unmanaged type in the constructor.
        public UnmanagedAttribute(UnmanagedType type)
        {
            thisType = type;
        }

        // Define a property to get and set the UnmanagedType value.
        public UnmanagedType Win32Type
        {
            get { return thisType; }
            set { thisType = Win32Type; }
        }
    }

    // Create a class for an imported Win32 unmanaged function.
    public class Win32 {
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        public static extern int MessageBox(int hWnd, String text,
            String caption, uint type);
    }

    public class AClass {
        // Add some attributes to Win32CallMethod.
        [Obsolete("This method is obsolete. Use managed MsgBox instead.")]
        [Unmanaged(UnmanagedType.User)]
        public void Win32CallMethod()
        {
            Win32.MessageBox(0, "This is an unmanaged call.", "Caution!", 0);
        }
    }

    class DemoClass {
        static void Main(string[] args)
            {
            // Get the AClass type to access its metadata.
            Type clsType = typeof(AClass);
            // Get the type information for Win32CallMethod.
            MethodInfo mInfo = clsType.GetMethod("Win32CallMethod");
            if (mInfo != null)
            {
                // Iterate through all the attributes of the method.
                foreach(Attribute attr in
                    Attribute.GetCustomAttributes(mInfo)) {
                    // Check for the Obsolete attribute.
                    if (attr.GetType() == typeof(ObsoleteAttribute))
                    {
                        Console.WriteLine("Method {0} is obsolete. " +
                            "The message is:",
                            mInfo.Name);
                        Console.WriteLine("  \"{0}\"",
                            ((ObsoleteAttribute)attr).Message);
                    }

                    // Check for the Unmanaged attribute.
                    else if (attr.GetType() == typeof(UnmanagedAttribute))
                    {
                        Console.WriteLine(
                            "This method calls unmanaged code.");
                        Console.WriteLine(
                            String.Format("The Unmanaged attribute type is {0}.",
                                          ((UnmanagedAttribute)attr).Win32Type));
                        AClass myCls = new AClass();
                        myCls.Win32CallMethod();
                    }
                }
            }
        }
    }
}

/*

This code example produces the following results.

First, the compilation yields the warning, "... This method is
obsolete. Use managed MsgBox instead."
Second, execution yields a message box with a title of "Caution!"
and message text of "This is an unmanaged call."
Third, the following text is displayed in the console window:

Method Win32CallMethod is obsolete. The message is:
  "This method is obsolete. Use managed MsgBox instead."
This method calls unmanaged code.
The Unmanaged attribute type is User.

*/

注解

返回值包含 element上级自定义属性。

备注

从 .NET Framework 版本 2.0 开始,如果属性以新的元数据格式存储,此方法将返回方法、构造函数和类型的安全属性。 使用版本 2.0 或更高版本编译的程序集使用新格式。 使用早期版本的 .NET Framework 编译的动态程序集和程序集使用旧的 XML 格式。 请参阅 发出声明性安全属性。

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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(Assembly)

Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs

检索应用于程序集的自定义特性的数组。 参数指定程序集。

C#
public static Attribute[] GetCustomAttributes (System.Reflection.Assembly element);

参数

element
Assembly

一个从 Assembly 类派生的对象,该类描述模块的可重用集合。

返回

一个 Attribute 数组,其中包含应用于 element的自定义属性;如果没有此类自定义属性,则为空数组。

例外

element null

示例

以下示例检索在当前程序集中找到的自定义属性。

C#
using System;
using System.Reflection;

[assembly: AssemblyTitle("CustAttrs1CS")]
[assembly: AssemblyDescription("GetCustomAttributes() Demo")]
[assembly: AssemblyCompany("Microsoft")]

class Example {
    static void Main() {
        // Get the Assembly object to access its metadata.
        Assembly assy = typeof(Example).Assembly;

        // Iterate through the attributes for the assembly.
        foreach(Attribute attr in Attribute.GetCustomAttributes(assy)) {
            // Check for the AssemblyTitle attribute.
            if (attr.GetType() == typeof(AssemblyTitleAttribute))
                Console.WriteLine("Assembly title is \"{0}\".",
                    ((AssemblyTitleAttribute)attr).Title);

            // Check for the AssemblyDescription attribute.
            else if (attr.GetType() ==
                typeof(AssemblyDescriptionAttribute))
                Console.WriteLine("Assembly description is \"{0}\".",
                    ((AssemblyDescriptionAttribute)attr).Description);

            // Check for the AssemblyCompany attribute.
            else if (attr.GetType() == typeof(AssemblyCompanyAttribute))
                Console.WriteLine("Assembly company is {0}.",
                    ((AssemblyCompanyAttribute)attr).Company);
        }
   }
}
// The example displays the following output:
//     Assembly title is "CustAttrs1CS".
//     Assembly description is "GetCustomAttributes() Demo".
//     Assembly company is Microsoft.

注解

备注

从 .NET Framework 版本 2.0 开始,如果属性以新的元数据格式存储,此方法将返回安全属性。 使用版本 2.0 或更高版本编译的程序集使用新格式。 使用早期版本的 .NET Framework 编译的动态程序集和程序集使用旧的 XML 格式。 请参阅 发出声明性安全属性。

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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(Module, Boolean)

Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs

检索应用于模块的自定义属性的数组。 参数指定模块和忽略的搜索选项。

C#
public static Attribute[] GetCustomAttributes (System.Reflection.Module element, bool inherit);

参数

element
Module

一个派生自描述可移植可执行文件的 Module 类的对象。

inherit
Boolean

忽略此参数,不会影响此方法的操作。

返回

一个 Attribute 数组,其中包含应用于 element的自定义属性;如果没有此类自定义属性,则为空数组。

例外

element null

示例

下面的代码示例演示如何使用 GetCustomAttributes,以 Module 作为参数。

C#
using System;
using System.Reflection;
using System.ComponentModel;

// Assign some attributes to the module.
[module:Description("A sample description")]

// Set the module's CLSCompliant attribute to false
// The CLSCompliant attribute is applicable for /target:module.
[module:CLSCompliant(false)]

namespace CustAttrs2CS {
    class DemoClass {
        static void Main(string[] args) {
            Type clsType = typeof(DemoClass);
            // Get the Module type to access its metadata.
            Module module = clsType.Module;

            // Iterate through all the attributes for the module.
            foreach(Attribute attr in Attribute.GetCustomAttributes(module)) {
                // Check for the Description attribute.
                if (attr.GetType() == typeof(DescriptionAttribute))
                    Console.WriteLine("Module {0} has the description " +
                        "\"{1}\".", module.Name,
                        ((DescriptionAttribute)attr).Description);
                // Check for the CLSCompliant attribute.
                else if (attr.GetType() == typeof(CLSCompliantAttribute))
                    Console.WriteLine("Module {0} {1} CLSCompliant.",
                        module.Name,
                        ((CLSCompliantAttribute)attr).IsCompliant ?
                            "is" : "is not");
            }
        }
    }
}

/*
 * Output:
 * Module CustAttrs2CS.exe is not CLSCompliant.
 * Module CustAttrs2CS.exe has the description "A sample description".
 */

注解

如果 inherittrue,则返回值包含 element 的上级自定义属性。

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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