System.Attribute.GetCustomAttribute 方法 (Assembly, Type)

方法描述

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

语法定义(C# System.Attribute.GetCustomAttribute 方法 (Assembly, Type) 的用法)

public static Attribute GetCustomAttribute(
	Assembly element,
	Type attributeType
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
element System-Reflection-Assembly 一个从 Assembly 类派生的对象,该类描述可重用模块集合。
attributeType System-Type 要搜索的自定义特性的类型,即基类型。
返回值 System.Attribute 一个引用,指向应用于 element 的 attributeType 类型的单个自定义特性;如果没有此类特性,则为 null。

提示和注释

如果希望返回多个值,则使用 GetCustomAttributes 方法,否则将引发 AmbiguousMatchException。

注意

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

System.Attribute.GetCustomAttribute 方法 (Assembly, Type)例子

下面的代码示例演示 GetCustomAttribute 方法的用法,采用 Assembly 作为参数。

using System;
using System.Reflection;

// Add an AssemblyDescription attribute
[assembly: AssemblyDescription("A sample description")]
namespace IsDef1CS
{
    public class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(DemoClass);
            // Get the assembly object.
            Assembly assy = clsType.Assembly;
            // Store the assembly's name.
            String assyName = assy.GetName().Name;
            // See if the Assembly Description is defined.
            bool isdef = Attribute.IsDefined(assy, 
                typeof(AssemblyDescriptionAttribute));
            if (isdef)
            {
                // Affirm that the attribute is defined.
                Console.WriteLine("The AssemblyDescription attribute " +
                    "is defined for assembly {0}.", assyName);
                // Get the description attribute itself.
                AssemblyDescriptionAttribute adAttr = 
                    (AssemblyDescriptionAttribute)Attribute.GetCustomAttribute(
                    assy, typeof(AssemblyDescriptionAttribute));
                // Display the description.
                if (adAttr != null)
                    Console.WriteLine("The description is \"{0}\".", 
                        adAttr.Description);
                else
                    Console.WriteLine("The description could not " +
                        "be retrieved.");            
            }
            else
                Console.WriteLine("The AssemblyDescription attribute is not " +
                    "defined for assembly {0}.", assyName);
        }
    }
}

/*
 * Output:
 * The AssemblyDescription attribute is defined for assembly IsDef1CS.
 * The description is "A sample description".
 */

异常

异常 异常描述
ArgumentNullException element 或 attributeType 为 null。
ArgumentException attributeType 不从 Attribute 派生。
AmbiguousMatchException 找到了多个请求的特性。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。