System.Attribute 类

方法描述

表示自定义特性的基类。

语法定义(C# System.Attribute 类 的用法)

[SerializableAttribute]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
[AttributeUsageAttribute(AttributeTargets.All, Inherited = true, AllowMultiple = false)]
[ComVisibleAttribute(true)]
public abstract class Attribute : _Attribute

构造函数

构造函数名称 构造函数描述
Attribute 初始化 Attribute 类的新实例。

成员/方法

方法名称 方法描述
Equals 基础结构。返回一个值,该值指示此实例是否与指定的对象相等。 (重写 Object.Equals(Object)。)
Finalize 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
GetCustomAttribute(Assembly, Type) 检索应用于指定程序集的自定义特性。 参数指定程序集和要搜索的自定义特性的类型。
GetCustomAttribute(MemberInfo, Type) 检索应用于类型成员的自定义特性。 参数指定成员和要搜索的自定义属性的类型。
GetCustomAttribute(Module, Type) 检索应用于模块的自定义特性。 参数指定模块和要搜索的自定义特性的类型。
GetCustomAttribute(ParameterInfo, Type) 检索应用于方法参数的自定义特性。 参数指定方法参数和要搜索的自定义特性的类型。
GetCustomAttribute(Assembly, Type, Boolean) 检索应用于程序集的自定义特性。 参数指定程序集、要搜索的自定义特性的类型以及忽略的搜索选项。
GetCustomAttribute(MemberInfo, Type, Boolean) 检索应用于类型成员的自定义特性。 参数指定成员、要搜索的自定义特性的类型以及是否搜索成员的祖先。
GetCustomAttribute(Module, Type, Boolean) 检索应用于模块的自定义特性。 参数指定模块、要搜索的自定义特性的类型以及忽略的搜索选项。
GetCustomAttribute(ParameterInfo, Type, Boolean) 检索应用于方法参数的自定义特性。 参数指定方法参数、要搜索的自定义特性的类型以及是否搜索方法参数的祖先。
GetCustomAttributes(Assembly) 检索应用于程序集的自定义特性的数组。 参数指定程序集。
GetCustomAttributes(MemberInfo) 检索应用于类型的成员的自定义特性的数组。 参数指定成员。
GetCustomAttributes(Module) 检索应用于模块的自定义特性的数组。 参数指定模块。
GetCustomAttributes(ParameterInfo) 检索应用于方法参数的自定义特性的数组。 参数指定方法参数。
GetCustomAttributes(Assembly, Boolean) 检索应用于程序集的自定义特性的数组。 参数指定程序集及忽略的搜索选项。
GetCustomAttributes(Assembly, Type) 检索应用于程序集的自定义特性的数组。 参数指定程序集,以及要搜索的自定义特性的类型。
GetCustomAttributes(MemberInfo, Boolean) 检索应用于类型的成员的自定义特性的数组。 参数指定成员、要搜索的自定义特性的类型以及是否搜索成员的祖先。
GetCustomAttributes(MemberInfo, Type) 检索应用于类型的成员的自定义特性的数组。 参数指定成员,以及要搜索的自定义特性的类型。
GetCustomAttributes(Module, Boolean) 检索应用于模块的自定义特性的数组。 参数指定模块及忽略的搜索选项。
GetCustomAttributes(Module, Type) 检索应用于模块的自定义特性的数组。 参数指定模块和要搜索的自定义特性的类型。
GetCustomAttributes(ParameterInfo, Boolean) 检索应用于方法参数的自定义特性的数组。 参数指定方法参数以及是否搜索方法参数的祖先。
GetCustomAttributes(ParameterInfo, Type) 检索应用于方法参数的自定义特性的数组。 参数指定方法参数和要搜索的自定义特性的类型。
GetCustomAttributes(Assembly, Type, Boolean) 检索应用于程序集的自定义特性的数组。 参数指定程序集、要搜索的自定义特性的类型以及忽略的搜索选项。
GetCustomAttributes(MemberInfo, Type, Boolean) 检索应用于类型的成员的自定义特性的数组。 参数指定成员、要搜索的自定义特性的类型以及是否搜索成员的祖先。
GetCustomAttributes(Module, Type, Boolean) 检索应用于模块的自定义特性的数组。 参数指定模块、要搜索的自定义特性的类型以及忽略的搜索选项。
GetCustomAttributes(ParameterInfo, Type, Boolean) 检索应用于方法参数的自定义特性的数组。 参数指定方法参数、要搜索的自定义特性的类型以及是否搜索方法参数的祖先。
GetHashCode 返回此实例的哈希代码。 (重写 Object.GetHashCode()。)
GetType 获取当前实例的 Type。 (继承自 Object。)
IsDefaultAttribute 当在派生类中重写时,指示此实例的值是否是派生类的默认值。
IsDefined(Assembly, Type) 确定是否将任意自定义特性应用于程序集。 参数指定程序集和要搜索的自定义特性的类型。
IsDefined(MemberInfo, Type) 确定是否将任意自定义特性应用于类型成员。 参数指定成员和要搜索的自定义特性的类型。
IsDefined(Module, Type) 确定是否将指定类型的任何自定义特性应用于模块。 参数指定模块和要搜索的自定义特性的类型。
IsDefined(ParameterInfo, Type) 确定是否将任意自定义特性应用于方法参数。 参数指定方法参数和要搜索的自定义特性的类型。
IsDefined(Assembly, Type, Boolean) 确定是否将任意自定义特性应用于程序集。 参数指定程序集、要搜索的自定义特性的类型以及忽略的搜索选项。
IsDefined(MemberInfo, Type, Boolean) 确定是否将任意自定义特性应用于类型成员。 参数指定成员、要搜索的自定义特性的类型以及是否搜索成员的祖先。
IsDefined(Module, Type, Boolean) 确定是否将任意自定义特性应用于模块。 参数指定模块、要搜索的自定义特性的类型以及忽略的搜索选项。
IsDefined(ParameterInfo, Type, Boolean) 确定是否将任意自定义特性应用于方法参数。 参数指定方法参数、要搜索的自定义特性的类型以及是否搜索方法参数的祖先。
Match 当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。
MemberwiseClone 创建当前 Object 的浅表副本。 (继承自 Object。)
ToString 返回表示当前对象的字符串。 (继承自 Object。)

提示和注释

Attribute 类将预定义的系统信息或用户定义的自定义信息与目标元素相关联。 目标元素可以是程序集、类、构造函数、委托、枚举、事件、字段、接口、方法、可移植可执行文件模块、参数、属性、返回值、结构或其他特性。

特性所提供的信息也称为元数据。 元数据可由应用程序在运行时进行检查以控制程序处理数据的方式,也可以由外部工具在运行前检查以控制应用程序处理或维护自身的方式。 例如,.NET Framework 预定义特性类型并使用特性类型控制运行时行为,某些编程语言使用特性类型表示 .NET Framework 常规类型系统不直接支持的语言功能。

所有特性类型都直接或间接地从 Attribute 类派生。 特性可应用于任何目标元素;多个特性可应用于同一目标元素;并且特性可由从目标元素派生的元素继承。 使用 AttributeTargets 类可以指定特性所应用到的目标元素。

Attribute 类提供检索和测试自定义特性的简便方法。 有关使用特性的更多信息,请参见应用特性和利用特性扩展元数据。

System.Attribute 类例子

下面的代码示例演示 Attribute 的用法。

using System;
using System.Reflection;

// An enumeration of animals. Start at 1 (0 = uninitialized).
public enum Animal {
    // Pets.
    Dog = 1,
    Cat,
    Bird,
}

// A custom attribute to allow a target to have a pet.
public class AnimalTypeAttribute : Attribute {
    // The constructor is called when the attribute is set.
    public AnimalTypeAttribute(Animal pet) {
        thePet = pet;
    }

    // Keep a variable internally ...
    protected Animal thePet;

    // .. and show a copy to the outside world.
    public Animal Pet {
        get { return thePet; }
        set { thePet = value; }
    }
}

// A test class where each method has its own pet.
class AnimalTypeTestClass {
    [AnimalType(Animal.Dog)]
    public void DogMethod() {}

    [AnimalType(Animal.Cat)]
    public void CatMethod() {}

    [AnimalType(Animal.Bird)]
    public void BirdMethod() {}
}

class DemoClass {
    static void Main(string[] args) {
        AnimalTypeTestClass testClass = new AnimalTypeTestClass();
        Type type = testClass.GetType();
        // Iterate through all the methods of the class.
        foreach(MethodInfo mInfo in type.GetMethods()) {
            // Iterate through all the Attributes for each method.
            foreach (Attribute attr in
                Attribute.GetCustomAttributes(mInfo)) {
                // Check for the AnimalType attribute.
                if (attr.GetType() == typeof(AnimalTypeAttribute))
                    Console.WriteLine(
                        "Method {0} has a pet {1} attribute.",
                        mInfo.Name, ((AnimalTypeAttribute)attr).Pet);
            }

        }
    }
}
/*
 * Output:
 * Method DogMethod has a pet Dog attribute.
 * Method CatMethod has a pet Cat attribute.
 * Method BirdMethod has a pet Bird attribute.
 */

继承层次结构

System.Object

System.Attribute

更多...

命名空间

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 系统要求。