C# 特性 Attribute

一、特性是什么?

特性是可以添加到编程元素(例如程序集、类型、成员和参数)的注释。 它们存储在程序集的元数据中,可以在运行时使用反射 API 时访问。

F12我们可以查看Attribute的定义

#region 程序集 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\mscorlib.dll
#endregion

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

namespace System
{
    //
    // 摘要:
    //     Represents the base class for custom attributes.
    [AttributeUsageAttribute(AttributeTargets.All, Inherited = true, AllowMultiple = false)]
    [ClassInterfaceAttribute(ClassInterfaceType.None)]
    [ComDefaultInterfaceAttribute(typeof(_Attribute))]
    [ComVisibleAttribute(true)]
    public abstract class Attribute : _Attribute
    {
        //
        // 摘要:
        //     Initializes a new instance of the System.Attribute class.
        protected Attribute();

        //
        // 摘要:
        //     When implemented in a derived class, gets a unique identifier for this System.Attribute.
        //
        // 返回结果:
        //     An System.Object that is a unique identifier for the attribute.
        public virtual object TypeId { get; }

        //
        // 摘要:
        //     Retrieves a custom attribute applied to a specified assembly. Parameters specify
        //     the assembly and the type of the custom attribute to search for.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.Assembly class that describes a
        //     reusable collection of modules.
        //
        //   attributeType:
        //     The type, or a base type, of the custom attribute to search for.
        //
        // 返回结果:
        //     A reference to the single custom attribute of type attributeType that is applied
        //     to element, or null if there is no such attribute.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element or attributeType is null.
        //
        //   T:System.ArgumentException:
        //     attributeType is not derived from System.Attribute.
        //
        //   T:System.Reflection.AmbiguousMatchException:
        //     More than one of the requested attributes was found.
        public static Attribute GetCustomAttribute(Assembly element, Type attributeType);
        //
        // 摘要:
        //     Retrieves a custom attribute applied to a member of a type. Parameters specify
        //     the member, and the type of the custom attribute to search for.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.MemberInfo class that describes
        //     a constructor, event, field, method, or property member of a class.
        //
        //   attributeType:
        //     The type, or a base type, of the custom attribute to search for.
        //
        // 返回结果:
        //     A reference to the single custom attribute of type attributeType that is applied
        //     to element, or null if there is no such attribute.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element or attributeType is null.
        //
        //   T:System.ArgumentException:
        //     attributeType is not derived from System.Attribute.
        //
        //   T:System.NotSupportedException:
        //     element is not a constructor, method, property, event, type, or field.
        //
        //   T:System.Reflection.AmbiguousMatchException:
        //     More than one of the requested attributes was found.
        //
        //   T:System.TypeLoadException:
        //     A custom attribute type cannot be loaded.
        public static Attribute GetCustomAttribute(MemberInfo element, Type attributeType);
        //
        // 摘要:
        //     Retrieves a custom attribute applied to a module. Parameters specify the module,
        //     and the type of the custom attribute to search for.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.Module class that describes a portable
        //     executable file.
        //
        //   attributeType:
        //     The type, or a base type, of the custom attribute to search for.
        //
        // 返回结果:
        //     A reference to the single custom attribute of type attributeType that is applied
        //     to element, or null if there is no such attribute.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element or attributeType is null.
        //
        //   T:System.ArgumentException:
        //     attributeType is not derived from System.Attribute.
        //
        //   T:System.Reflection.AmbiguousMatchException:
        //     More than one of the requested attributes was found.
        public static Attribute GetCustomAttribute(Module element, Type attributeType);
        //
        // 摘要:
        //     Retrieves a custom attribute applied to a method parameter. Parameters specify
        //     the method parameter, and the type of the custom attribute to search for.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.ParameterInfo class that describes
        //     a parameter of a member of a class.
        //
        //   attributeType:
        //     The type, or a base type, of the custom attribute to search for.
        //
        // 返回结果:
        //     A reference to the single custom attribute of type attributeType that is applied
        //     to element, or null if there is no such attribute.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element or attributeType is null.
        //
        //   T:System.ArgumentException:
        //     attributeType is not derived from System.Attribute.
        //
        //   T:System.Reflection.AmbiguousMatchException:
        //     More than one of the requested attributes was found.
        //
        //   T:System.TypeLoadException:
        //     A custom attribute type cannot be loaded.
        public static Attribute GetCustomAttribute(ParameterInfo element, Type attributeType);
        //
        // 摘要:
        //     Retrieves a custom attribute applied to an assembly. Parameters specify the assembly,
        //     the type of the custom attribute to search for, and an ignored search option.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.Assembly class that describes a
        //     reusable collection of modules.
        //
        //   attributeType:
        //     The type, or a base type, of the custom attribute to search for.
        //
        //   inherit:
        //     This parameter is ignored, and does not affect the operation of this method.
        //
        // 返回结果:
        //     A reference to the single custom attribute of type attributeType that is applied
        //     to element, or null if there is no such attribute.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element or attributeType is null.
        //
        //   T:System.ArgumentException:
        //     attributeType is not derived from System.Attribute.
        //
        //   T:System.Reflection.AmbiguousMatchException:
        //     More than one of the requested attributes was found.
        public static Attribute GetCustomAttribute(Assembly element, Type attributeType, bool inherit);
        //
        // 摘要:
        //     Retrieves a custom attribute applied to a member of a type. Parameters specify
        //     the member, the type of the custom attribute to search for, and whether to search
        //     ancestors of the member.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.MemberInfo class that describes
        //     a constructor, event, field, method, or property member of a class.
        //
        //   attributeType:
        //     The type, or a base type, of the custom attribute to search for.
        //
        //   inherit:
        //     If true, specifies to also search the ancestors of element for custom attributes.
        //
        // 返回结果:
        //     A reference to the single custom attribute of type attributeType that is applied
        //     to element, or null if there is no such attribute.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element or attributeType is null.
        //
        //   T:System.ArgumentException:
        //     attributeType is not derived from System.Attribute.
        //
        //   T:System.NotSupportedException:
        //     element is not a constructor, method, property, event, type, or field.
        //
        //   T:System.Reflection.AmbiguousMatchException:
        //     More than one of the requested attributes was found.
        //
        //   T:System.TypeLoadException:
        //     A custom attribute type cannot be loaded.
        public static Attribute GetCustomAttribute(MemberInfo element, Type attributeType, bool inherit);
        //
        // 摘要:
        //     Retrieves a custom attribute applied to a module. Parameters specify the module,
        //     the type of the custom attribute to search for, and an ignored search option.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.Module class that describes a portable
        //     executable file.
        //
        //   attributeType:
        //     The type, or a base type, of the custom attribute to search for.
        //
        //   inherit:
        //     This parameter is ignored, and does not affect the operation of this method.
        //
        // 返回结果:
        //     A reference to the single custom attribute of type attributeType that is applied
        //     to element, or null if there is no such attribute.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element or attributeType is null.
        //
        //   T:System.ArgumentException:
        //     attributeType is not derived from System.Attribute.
        //
        //   T:System.Reflection.AmbiguousMatchException:
        //     More than one of the requested attributes was found.
        public static Attribute GetCustomAttribute(Module element, Type attributeType, bool inherit);
        //
        // 摘要:
        //     Retrieves a custom attribute applied to a method parameter. Parameters specify
        //     the method parameter, the type of the custom attribute to search for, and whether
        //     to search ancestors of the method parameter.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.ParameterInfo class that describes
        //     a parameter of a member of a class.
        //
        //   attributeType:
        //     The type, or a base type, of the custom attribute to search for.
        //
        //   inherit:
        //     If true, specifies to also search the ancestors of element for custom attributes.
        //
        // 返回结果:
        //     A reference to the single custom attribute of type attributeType that is applied
        //     to element, or null if there is no such attribute.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element or attributeType is null.
        //
        //   T:System.ArgumentException:
        //     attributeType is not derived from System.Attribute.
        //
        //   T:System.Reflection.AmbiguousMatchException:
        //     More than one of the requested attributes was found.
        //
        //   T:System.TypeLoadException:
        //     A custom attribute type cannot be loaded.
        public static Attribute GetCustomAttribute(ParameterInfo element, Type attributeType, bool inherit);
        //
        // 摘要:
        //     Retrieves an array of the custom attributes applied to a member of a type. A
        //     parameter specifies the member.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.MemberInfo class that describes
        //     a constructor, event, field, method, or property member of a class.
        //
        // 返回结果:
        //     An System.Attribute array that contains the custom attributes applied to element,
        //     or an empty array if no such custom attributes exist.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element is null.
        //
        //   T:System.NotSupportedException:
        //     element is not a constructor, method, property, event, type, or field.
        //
        //   T:System.TypeLoadException:
        //     A custom attribute type cannot be loaded.
        public static Attribute[] GetCustomAttributes(MemberInfo element);
        //
        // 摘要:
        //     Retrieves an array of the custom attributes applied to a method parameter. A
        //     parameter specifies the method parameter.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.ParameterInfo class that describes
        //     a parameter of a member of a class.
        //
        // 返回结果:
        //     An System.Attribute array that contains the custom attributes applied to element,
        //     or an empty array if no such custom attributes exist.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element is null.
        //
        //   T:System.TypeLoadException:
        //     A custom attribute type cannot be loaded.
        public static Attribute[] GetCustomAttributes(ParameterInfo element);
        //
        // 摘要:
        //     Retrieves an array of the custom attributes applied to an assembly. A parameter
        //     specifies the assembly.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.Assembly class that describes a
        //     reusable collection of modules.
        //
        // 返回结果:
        //     An System.Attribute array that contains the custom attributes applied to element,
        //     or an empty array if no such custom attributes exist.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element is null.
        public static Attribute[] GetCustomAttributes(Assembly element);
        //
        // 摘要:
        //     Retrieves an array of the custom attributes applied to a module. A parameter
        //     specifies the module.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.Module class that describes a portable
        //     executable file.
        //
        // 返回结果:
        //     An System.Attribute array that contains the custom attributes applied to element,
        //     or an empty array if no such custom attributes exist.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element is null.
        public static Attribute[] GetCustomAttributes(Module element);
        //
        // 摘要:
        //     Retrieves an array of the custom attributes applied to an assembly. Parameters
        //     specify the assembly, and an ignored search option.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.Assembly class that describes a
        //     reusable collection of modules.
        //
        //   inherit:
        //     This parameter is ignored, and does not affect the operation of this method.
        //
        // 返回结果:
        //     An System.Attribute array that contains the custom attributes applied to element,
        //     or an empty array if no such custom attributes exist.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element or attributeType is null.
        public static Attribute[] GetCustomAttributes(Assembly element, bool inherit);
        //
        // 摘要:
        //     Retrieves an array of the custom attributes applied to an assembly. Parameters
        //     specify the assembly, and the type of the custom attribute to search for.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.Assembly class that describes a
        //     reusable collection of modules.
        //
        //   attributeType:
        //     The type, or a base type, of the custom attribute to search for.
        //
        // 返回结果:
        //     An System.Attribute array that contains the custom attributes of type attributeType
        //     applied to element, or an empty array if no such custom attributes exist.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element or attributeType is null.
        //
        //   T:System.ArgumentException:
        //     attributeType is not derived from System.Attribute.
        public static Attribute[] GetCustomAttributes(Assembly element, Type attributeType);
        //
        // 摘要:
        //     Retrieves an array of the custom attributes applied to a module. Parameters specify
        //     the module, and an ignored search option.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.Module class that describes a portable
        //     executable file.
        //
        //   inherit:
        //     This parameter is ignored, and does not affect the operation of this method.
        //
        // 返回结果:
        //     An System.Attribute array that contains the custom attributes applied to element,
        //     or an empty array if no such custom attributes exist.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element or attributeType is null.
        public static Attribute[] GetCustomAttributes(Module element, bool inherit);
        //
        // 摘要:
        //     Retrieves an array of the custom attributes applied to a member of a type. Parameters
        //     specify the member, and the type of the custom attribute to search for.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.MemberInfo class that describes
        //     a constructor, event, field, method, or property member of a class.
        //
        //   type:
        //     The type, or a base type, of the custom attribute to search for.
        //
        // 返回结果:
        //     An System.Attribute array that contains the custom attributes of type type applied
        //     to element, or an empty array if no such custom attributes exist.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element or type is null.
        //
        //   T:System.ArgumentException:
        //     type is not derived from System.Attribute.
        //
        //   T:System.NotSupportedException:
        //     element is not a constructor, method, property, event, type, or field.
        //
        //   T:System.TypeLoadException:
        //     A custom attribute type cannot be loaded.
        public static Attribute[] GetCustomAttributes(MemberInfo element, Type type);
        //
        // 摘要:
        //     Retrieves an array of the custom attributes applied to a member of a type. Parameters
        //     specify the member, the type of the custom attribute to search for, and whether
        //     to search ancestors of the member.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.MemberInfo class that describes
        //     a constructor, event, field, method, or property member of a class.
        //
        //   inherit:
        //     If true, specifies to also search the ancestors of element for custom attributes.
        //
        // 返回结果:
        //     An System.Attribute array that contains the custom attributes applied to element,
        //     or an empty array if no such custom attributes exist.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element is null.
        //
        //   T:System.NotSupportedException:
        //     element is not a constructor, method, property, event, type, or field.
        //
        //   T:System.TypeLoadException:
        //     A custom attribute type cannot be loaded.
        public static Attribute[] GetCustomAttributes(MemberInfo element, bool inherit);
        //
        // 摘要:
        //     Retrieves an array of the custom attributes applied to a method parameter. Parameters
        //     specify the method parameter, and whether to search ancestors of the method parameter.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.ParameterInfo class that describes
        //     a parameter of a member of a class.
        //
        //   inherit:
        //     If true, specifies to also search the ancestors of element for custom attributes.
        //
        // 返回结果:
        //     An System.Attribute array that contains the custom attributes applied to element,
        //     or an empty array if no such custom attributes exist.
        //
        // 异常:
        //   T:System.ArgumentException:
        //     The System.Reflection.ParameterInfo.Member property of element is null.
        //
        //   T:System.ArgumentNullException:
        //     element is null.
        //
        //   T:System.TypeLoadException:
        //     A custom attribute type cannot be loaded.
        public static Attribute[] GetCustomAttributes(ParameterInfo element, bool inherit);
        //
        // 摘要:
        //     Retrieves an array of the custom attributes applied to a method parameter. Parameters
        //     specify the method parameter, and the type of the custom attribute to search
        //     for.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.ParameterInfo class that describes
        //     a parameter of a member of a class.
        //
        //   attributeType:
        //     The type, or a base type, of the custom attribute to search for.
        //
        // 返回结果:
        //     An System.Attribute array that contains the custom attributes of type attributeType
        //     applied to element, or an empty array if no such custom attributes exist.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element or attributeType is null.
        //
        //   T:System.ArgumentException:
        //     attributeType is not derived from System.Attribute.
        //
        //   T:System.TypeLoadException:
        //     A custom attribute type cannot be loaded.
        public static Attribute[] GetCustomAttributes(ParameterInfo element, Type attributeType);
        //
        // 摘要:
        //     Retrieves an array of the custom attributes applied to a module. Parameters specify
        //     the module, and the type of the custom attribute to search for.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.Module class that describes a portable
        //     executable file.
        //
        //   attributeType:
        //     The type, or a base type, of the custom attribute to search for.
        //
        // 返回结果:
        //     An System.Attribute array that contains the custom attributes of type attributeType
        //     applied to element, or an empty array if no such custom attributes exist.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element or attributeType is null.
        //
        //   T:System.ArgumentException:
        //     attributeType is not derived from System.Attribute.
        public static Attribute[] GetCustomAttributes(Module element, Type attributeType);
        //
        // 摘要:
        //     Retrieves an array of the custom attributes applied to a member of a type. Parameters
        //     specify the member, the type of the custom attribute to search for, and whether
        //     to search ancestors of the member.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.MemberInfo class that describes
        //     a constructor, event, field, method, or property member of a class.
        //
        //   type:
        //     The type, or a base type, of the custom attribute to search for.
        //
        //   inherit:
        //     If true, specifies to also search the ancestors of element for custom attributes.
        //
        // 返回结果:
        //     An System.Attribute array that contains the custom attributes of type type applied
        //     to element, or an empty array if no such custom attributes exist.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element or type is null.
        //
        //   T:System.ArgumentException:
        //     type is not derived from System.Attribute.
        //
        //   T:System.NotSupportedException:
        //     element is not a constructor, method, property, event, type, or field.
        //
        //   T:System.TypeLoadException:
        //     A custom attribute type cannot be loaded.
        public static Attribute[] GetCustomAttributes(MemberInfo element, Type type, bool inherit);
        //
        // 摘要:
        //     Retrieves an array of the custom attributes applied to a module. Parameters specify
        //     the module, the type of the custom attribute to search for, and an ignored search
        //     option.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.Module class that describes a portable
        //     executable file.
        //
        //   attributeType:
        //     The type, or a base type, of the custom attribute to search for.
        //
        //   inherit:
        //     This parameter is ignored, and does not affect the operation of this method.
        //
        // 返回结果:
        //     An System.Attribute array that contains the custom attributes of type attributeType
        //     applied to element, or an empty array if no such custom attributes exist.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element or attributeType is null.
        //
        //   T:System.ArgumentException:
        //     attributeType is not derived from System.Attribute.
        public static Attribute[] GetCustomAttributes(Module element, Type attributeType, bool inherit);
        //
        // 摘要:
        //     Retrieves an array of the custom attributes applied to a method parameter. Parameters
        //     specify the method parameter, the type of the custom attribute to search for,
        //     and whether to search ancestors of the method parameter.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.ParameterInfo class that describes
        //     a parameter of a member of a class.
        //
        //   attributeType:
        //     The type, or a base type, of the custom attribute to search for.
        //
        //   inherit:
        //     If true, specifies to also search the ancestors of element for custom attributes.
        //
        // 返回结果:
        //     An System.Attribute array that contains the custom attributes of type attributeType
        //     applied to element, or an empty array if no such custom attributes exist.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element or attributeType is null.
        //
        //   T:System.ArgumentException:
        //     attributeType is not derived from System.Attribute.
        //
        //   T:System.TypeLoadException:
        //     A custom attribute type cannot be loaded.
        public static Attribute[] GetCustomAttributes(ParameterInfo element, Type attributeType, bool inherit);
        //
        // 摘要:
        //     Retrieves an array of the custom attributes applied to an assembly. Parameters
        //     specify the assembly, the type of the custom attribute to search for, and an
        //     ignored search option.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.Assembly class that describes a
        //     reusable collection of modules.
        //
        //   attributeType:
        //     The type, or a base type, of the custom attribute to search for.
        //
        //   inherit:
        //     This parameter is ignored, and does not affect the operation of this method.
        //
        // 返回结果:
        //     An System.Attribute array that contains the custom attributes of type attributeType
        //     applied to element, or an empty array if no such custom attributes exist.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element or attributeType is null.
        //
        //   T:System.ArgumentException:
        //     attributeType is not derived from System.Attribute.
        public static Attribute[] GetCustomAttributes(Assembly element, Type attributeType, bool inherit);
        //
        // 摘要:
        //     Determines whether any custom attributes of a specified type are applied to a
        //     module. Parameters specify the module, and the type of the custom attribute to
        //     search for.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.Module class that describes a portable
        //     executable file.
        //
        //   attributeType:
        //     The type, or a base type, of the custom attribute to search for.
        //
        // 返回结果:
        //     true if a custom attribute of type attributeType is applied to element; otherwise,
        //     false.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element or attributeType is null.
        //
        //   T:System.ArgumentException:
        //     attributeType is not derived from System.Attribute.
        public static bool IsDefined(Module element, Type attributeType);
        //
        // 摘要:
        //     Determines whether any custom attributes are applied to a method parameter. Parameters
        //     specify the method parameter, and the type of the custom attribute to search
        //     for.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.ParameterInfo class that describes
        //     a parameter of a member of a class.
        //
        //   attributeType:
        //     The type, or a base type, of the custom attribute to search for.
        //
        // 返回结果:
        //     true if a custom attribute of type attributeType is applied to element; otherwise,
        //     false.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element or attributeType is null.
        //
        //   T:System.ArgumentException:
        //     attributeType is not derived from System.Attribute.
        public static bool IsDefined(ParameterInfo element, Type attributeType);
        //
        // 摘要:
        //     Determines whether any custom attributes are applied to an assembly. Parameters
        //     specify the assembly, and the type of the custom attribute to search for.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.Assembly class that describes a
        //     reusable collection of modules.
        //
        //   attributeType:
        //     The type, or a base type, of the custom attribute to search for.
        //
        // 返回结果:
        //     true if a custom attribute of type attributeType is applied to element; otherwise,
        //     false.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element or attributeType is null.
        //
        //   T:System.ArgumentException:
        //     attributeType is not derived from System.Attribute.
        public static bool IsDefined(Assembly element, Type attributeType);
        //
        // 摘要:
        //     Determines whether any custom attributes are applied to a member of a type. Parameters
        //     specify the member, and the type of the custom attribute to search for.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.MemberInfo class that describes
        //     a constructor, event, field, method, type, or property member of a class.
        //
        //   attributeType:
        //     The type, or a base type, of the custom attribute to search for.
        //
        // 返回结果:
        //     true if a custom attribute of type attributeType is applied to element; otherwise,
        //     false.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element or attributeType is null.
        //
        //   T:System.ArgumentException:
        //     attributeType is not derived from System.Attribute.
        //
        //   T:System.NotSupportedException:
        //     element is not a constructor, method, property, event, type, or field.
        public static bool IsDefined(MemberInfo element, Type attributeType);
        //
        // 摘要:
        //     Determines whether any custom attributes are applied to a member of a type. Parameters
        //     specify the member, the type of the custom attribute to search for, and whether
        //     to search ancestors of the member.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.MemberInfo class that describes
        //     a constructor, event, field, method, type, or property member of a class.
        //
        //   attributeType:
        //     The type, or a base type, of the custom attribute to search for.
        //
        //   inherit:
        //     If true, specifies to also search the ancestors of element for custom attributes.
        //
        // 返回结果:
        //     true if a custom attribute of type attributeType is applied to element; otherwise,
        //     false.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element or attributeType is null.
        //
        //   T:System.ArgumentException:
        //     attributeType is not derived from System.Attribute.
        //
        //   T:System.NotSupportedException:
        //     element is not a constructor, method, property, event, type, or field.
        public static bool IsDefined(MemberInfo element, Type attributeType, bool inherit);
        //
        // 摘要:
        //     Determines whether any custom attributes are applied to a module. Parameters
        //     specify the module, the type of the custom attribute to search for, and an ignored
        //     search option.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.Module class that describes a portable
        //     executable file.
        //
        //   attributeType:
        //     The type, or a base type, of the custom attribute to search for.
        //
        //   inherit:
        //     This parameter is ignored, and does not affect the operation of this method.
        //
        // 返回结果:
        //     true if a custom attribute of type attributeType is applied to element; otherwise,
        //     false.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element or attributeType is null.
        //
        //   T:System.ArgumentException:
        //     attributeType is not derived from System.Attribute.
        public static bool IsDefined(Module element, Type attributeType, bool inherit);
        //
        // 摘要:
        //     Determines whether any custom attributes are applied to an assembly. Parameters
        //     specify the assembly, the type of the custom attribute to search for, and an
        //     ignored search option.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.Assembly class that describes a
        //     reusable collection of modules.
        //
        //   attributeType:
        //     The type, or a base type, of the custom attribute to search for.
        //
        //   inherit:
        //     This parameter is ignored, and does not affect the operation of this method.
        //
        // 返回结果:
        //     true if a custom attribute of type attributeType is applied to element; otherwise,
        //     false.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element or attributeType is null.
        //
        //   T:System.ArgumentException:
        //     attributeType is not derived from System.Attribute.
        public static bool IsDefined(Assembly element, Type attributeType, bool inherit);
        //
        // 摘要:
        //     Determines whether any custom attributes are applied to a method parameter. Parameters
        //     specify the method parameter, the type of the custom attribute to search for,
        //     and whether to search ancestors of the method parameter.
        //
        // 参数:
        //   element:
        //     An object derived from the System.Reflection.ParameterInfo class that describes
        //     a parameter of a member of a class.
        //
        //   attributeType:
        //     The type, or a base type, of the custom attribute to search for.
        //
        //   inherit:
        //     If true, specifies to also search the ancestors of element for custom attributes.
        //
        // 返回结果:
        //     true if a custom attribute of type attributeType is applied to element; otherwise,
        //     false.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     element or attributeType is null.
        //
        //   T:System.ArgumentException:
        //     attributeType is not derived from System.Attribute.
        //
        //   T:System.ExecutionEngineException:
        //     element is not a method, constructor, or type.
        public static bool IsDefined(ParameterInfo element, Type attributeType, bool inherit);
        //
        // 摘要:
        //     Returns a value that indicates whether this instance is equal to a specified
        //     object.
        //
        // 参数:
        //   obj:
        //     An System.Object to compare with this instance or null.
        //
        // 返回结果:
        //     true if obj equals the type and value of this instance; otherwise, false.
        [SecuritySafeCriticalAttribute]
        public override bool Equals(object obj);
        //
        // 摘要:
        //     Returns the hash code for this instance.
        //
        // 返回结果:
        //     A 32-bit signed integer hash code.
        [SecuritySafeCriticalAttribute]
        public override int GetHashCode();
        //
        // 摘要:
        //     When overridden in a derived class, indicates whether the value of this instance
        //     is the default value for the derived class.
        //
        // 返回结果:
        //     true if this instance is the default attribute for the class; otherwise, false.
        public virtual bool IsDefaultAttribute();
        //
        // 摘要:
        //     When overridden in a derived class, returns a value that indicates whether this
        //     instance equals a specified object.
        //
        // 参数:
        //   obj:
        //     An System.Object to compare with this instance of System.Attribute.
        //
        // 返回结果:
        //     true if this instance equals obj; otherwise, false.
        public virtual bool Match(object obj);
    }
}
Attribute

通过定义我们可以看到特性的本质其实是一个类,所有的特性都直接或间接继承Attribute。

一般特性以“Attribute”结尾,在书写时可以省略Attribute,如果不是以Attribute结尾则不能省略。

二、特性与注释

注释:增加注释后,在Visual Studio中可以看到方法的注释说明,但在程序中无法获取。

特性:AttributeUsageAttribute、ObsoleteAttribute等可以影响编译器,SerializableAttribute、HttpPostAttribute、HttpGetAttribute、FromBodyAttribute等可以影响程序运行。

三、特性的使用

一)几个特殊特性

1、ObsoleteAttribute 如图在UserModel上标记ObsoleteAttribute特性并指定error为true时,可以强制编译器报错

C# 特性 Attribute_第1张图片

#region 程序集 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\mscorlib.dll
#endregion

using System.Runtime.InteropServices;

namespace System
{
    //
    // 摘要:
    //     标记不再使用的程序元素。此类不能被继承。
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Interface | AttributeTargets.Delegate, Inherited = false)]
    [ComVisible(true)]
    public sealed class ObsoleteAttribute : Attribute
    {
        //
        // 摘要:
        //     使用默认属性初始化 System.ObsoleteAttribute 类的新实例。
        public ObsoleteAttribute();
        //
        // 摘要:
        //     使用指定的变通方法消息初始化 System.ObsoleteAttribute 类的新实例。
        //
        // 参数:
        //   message:
        //     描述可选的变通方法的文本字符串。
        public ObsoleteAttribute(string message);
        //
        // 摘要:
        //     使用变通方法消息和布尔值初始化 System.ObsoleteAttribute 类的新实例,该布尔值指示是否将使用已过时的元素视为错误。
        //
        // 参数:
        //   message:
        //     描述可选的变通方法的文本字符串。
        //
        //   error:
        //     指示是否将使用已过时的元素视为错误的布尔值。
        public ObsoleteAttribute(string message, bool error);

        //
        // 摘要:
        //     获取指示编译器是否将使用已过时的程序元素视为错误的布尔值。
        //
        // 返回结果:
        //     如果将使用已过时的元素视为错误,则为 true;否则为 false。默认值为 false。
        public bool IsError { get; }
        //
        // 摘要:
        //     获取变通方法消息,包括对可选程序元素的说明。
        //
        // 返回结果:
        //     变通方法文本字符串。
        public string Message { get; }
    }
}
ObsoleteAttribute

2、AttributeUsageAttribute 修饰特性的特性 ValidOn指定特性可以使用的范围  默认值为 All,AllowMultiple标记特性是否可以重复修饰 默认值为 false,Inherited标记特性是否可以被继承 默认值为 true

#region 程序集 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\mscorlib.dll
#endregion

using System.Runtime.InteropServices;

namespace System
{
    //
    // 摘要:
    //     指定另一特性类的用法。此类不能被继承。
    [AttributeUsage(AttributeTargets.Class, Inherited = true)]
    [ComVisible(true)]
    public sealed class AttributeUsageAttribute : Attribute
    {
        //
        // 摘要:
        //     用指定的 System.AttributeTargets、System.AttributeUsageAttribute.AllowMultiple 值和
        //     System.AttributeUsageAttribute.Inherited 值列表初始化 System.AttributeUsageAttribute
        //     类的新实例。
        //
        // 参数:
        //   validOn:
        //     使用按位"或"运算符组合的一组值,用于指示哪些程序元素是有效的。
        public AttributeUsageAttribute(AttributeTargets validOn);

        //
        // 摘要:
        //     获取或设置一个布尔值,该值指示能否为一个程序元素指定多个指示属性实例。
        //
        // 返回结果:
        //     如果允许指定多个实例,则为 true;否则为 false。默认值为 false。
        public bool AllowMultiple { get; set; }
        //
        // 摘要:
        //     获取或设置一个布尔值,该值指示指示的属性能否由派生类和重写成员继承。
        //
        // 返回结果:
        //     如果该属性可由派生类和重写成员继承,则为 true,否则为 false。默认值为 true。
        public bool Inherited { get; set; }
        //
        // 摘要:
        //     获取一组值,这组值标识指示的属性可应用到的程序元素。
        //
        // 返回结果:
        //     一个或多个 System.AttributeTargets 值。默认值为 All。
        public AttributeTargets ValidOn { get; }
    }
}
AttributeUsageAttribute

二)使用范围

    [AttributeUsage(AttributeTargets.All,AllowMultiple =true,Inherited =true)]
    public class CustomAttribute:Attribute
    {
        public CustomAttribute()
        {

        }
        public CustomAttribute(int Id)
        {
            this._id = Id;
        }
        public int _id;
        public string Name { get; set; }
        public int Show()
        {
            return this._id;
        }
    }
View Code
    [Custom]//修饰类
    public class AttributeTest
    {
        [Custom]//修饰字段
        public string field;
        [Custom]//修饰属性
        public string Property { get; set; }

        [return: Custom]//修饰返回值 可以被堆叠修饰,也可以用‘,’隔开修饰
        [Custom,Custom(1)]//修饰方法,可以被同一特性重复修饰
        public string DoNothing([Custom]string test)//修饰方法参数
        {
            return test;
        }
        [Custom]//修饰接口
        public interface ITestInterFace { }
        [Custom]//修饰委托
        public delegate void TestDelegate();
        [Custom]//修饰事件
        public event TestDelegate TestHandle;
    }
View Code

三)特性的使用

特性单独使用,视乎并没有作用,在方法、属性、字段上标记特性,对程序并没有影响,如果把反射结合起来,特性就会产生巨大的价值。

    public class DisplayNameAttribute:Attribute
    {
        public DisplayNameAttribute(string displayName)
        {
            this.DisplayName = displayName;
        }
        public string DisplayName;
    }
DisplayNameAttribute
    [DisplayName("用户状态")]
    public enum UserState
    {
        /// 
        /// 正常
        /// 
        [DisplayName("正常")]
        Normal = 0,
        /// 
        /// 冻结
        /// 
        [DisplayName("冻结")]
        Frozen = 1,
        /// 
        /// 删除
        /// 
        [DisplayName("删除")]
        Deleted = 2
    }
UserState
    public class TableNameAttribute:Attribute
    {
        public TableNameAttribute(string tableName)
        {
            this.TableName = tableName;
        }
        public string TableName;
    }
TableNameAttribute
    [Serializable]
    [TableName("User")]
    public class UserModel : BaseModel
    {
        /// 
        /// Name
        /// 
        [DisplayName("姓名")]
        public string Name { get; set; }

        /// 
        /// Account
        /// 
        [DisplayName("账户")]
        [Required]
        public string Account { get; set; }

        /// 
        /// Password
        /// 
        [DisplayName("密码")]
        [Required]
        [StringLength(6,16)]
        public string Password { get; set; }

        /// 
        /// Email
        /// 
        [DisplayName("邮箱")]
        [Email]
        public string Email { get; set; }

        /// 
        /// Mobile
        /// 
        [DisplayName("手机号")]
        [Mobile]
        public string Mobile { get; set; }

        /// 
        /// CompanyId
        /// 
        [DisplayName("公司编号")]
        public int? CompanyId { get; set; }

        /// 
        /// CompanyName
        /// 
        [DisplayName("公司名称")]
        public string CompanyName { get; set; }

        /// 
        /// 用户状态  0正常 1冻结 2删除
        /// 
        [DisplayName("用户状态")]
        [TableName("State")]
        [Required]
        public int Status { get; set; }

        /// 
        /// 用户类型  1 普通用户 2管理员 4超级管理员
        /// 
        [DisplayName("用户类型")]
        [Required]
        public int UserType { get; set; }

        /// 
        /// LastLoginTime
        /// 
        [DisplayName("最近登录时间")]
        public DateTime? LastLoginTime { get; set; }

        /// 
        /// CreateTime
        /// 
        [DisplayName("创建时间")]
        [Required]
        public DateTime CreateTime { get; set; }

        /// 
        /// CreatorId
        /// 
        [DisplayName("关联模式")]
        [Required]
        public int CreatorId { get; set; }

        /// 
        /// LastModifierId
        /// 
        [DisplayName("最后修改编号")]
        public int? LastModifierId { get; set; }

        /// 
        /// LastModifyTime
        /// 
        [DisplayName("最后修改时间")]
        public DateTime? LastModifyTime { get; set; }

    }
UserModel
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;


    /// 
    /// 反射帮助类
    /// 
    public static class ReflectionHelper
    {
        /// 
        /// 获取成员描述信息
        /// 
        /// 成员
        /// 描述信息
        public static string GetDescription(this MemberInfo memberInfo)
        {
            return memberInfo.GetDescription(attribute => attribute.Description);
        }
        /// 
        /// 获取成员描述信息
        /// 
        /// 特性类型
        /// 成员
        /// 特性获取描述的委托
        /// 描述信息
        public static string GetDescription(this MemberInfo memberInfo, Funcstring> func) where T : Attribute
        {
            if (memberInfo.IsDefined(typeof(T), true))
            {
                object[] attributes = memberInfo.GetCustomAttributes(typeof(T), true);
                foreach (T attribute in attributes)
                {
                    return func(attribute);
                }
            }
            return memberInfo.Name;
        }
    }
ReflectionHelper 反射帮助类
    /// 
    /// 枚举描述信息帮助类
    /// 
    /// 
    public static class EnumHelper
    {
        private static Dictionarystring> dict = new Dictionarystring>();
        /// 
        /// 枚举描述信息
        /// 
        /// 
        /// 
        public static string GetDescription(this System.Enum t)
        {
            if (!dict.Keys.Contains(t))
            {
                Type type = t.GetType();
                string name = System.Enum.GetName(type, t);
                dict.Add(t, type.GetField(name).GetDescription());
            }
            return dict[t];
        }
        /// 
        /// 枚举描述信息
        /// 
        /// 
        /// 
        public static string GetDescription(this System.Enum t,Funcstring> func)where T:Attribute
        {
            Type type = t.GetType();
            string name = System.Enum.GetName(type, t);
            return ReflectionHelper.GetDescription(type.GetField(name), func);
        }
    }
EnumHelper
            typeof(UserModel).GetDescription(attribute => attribute.TableName);//获取UserModel类表名称
            typeof(UserState).GetDescription(attribute => attribute.DisplayName);//获取UserState枚举类型的描述信息
            UserState.Normal.GetDescription(attribute=>attribute.DisplayName);//获取枚举的描述信息
            
View Code

 本文参考文档:https://www.cnblogs.com/loverwangshan/p/10153832.html;

微软文档地址:https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/attributes;

https://docs.microsoft.com/zh-cn/dotnet/standard/design-guidelines/attributes;

你可能感兴趣的:(C# 特性 Attribute)