C#中的特性(attribute)

 

一、什么是特性

a)         特性的定义

MSDN特性是被指定给某一声明的一则附加的声明性信息。  

b)        我们曾经见过的特性有哪些?

                         i.              Serializable      Localizable()   AuthorAttribute()  Obsolete()      Conditional()      DefaultValue() WebMethod       ScriptService      DllImport()……

二、特性的应用目标及方式

a)         特性可以应用的目标元素可以为:程序集(assembly)、模块(module)、类型(type)、属性(property)、事件(event)、字段(field)、方法(method)、参数(param)、返回值(return)

b)        特性以[,]形式展现,放在紧挨着的元素上,多个特性可以应用于同一元素,特性间以逗号隔开,以下表达规则有效:[AttributeUsage][ Flags][AttributeUsage, Flags][Flags, AttibuteUsageAttribute][AttributeUsage(), FlagesAttribute()]

三、特性的分类

a)         预定义(Pre-defined)特性,系统内置定义特性

b)        定制特性(custom attributes),自定义特性

四、预定义特性的应用实例

 

例子一:程序集信息定义(AssemblyInfo.cs文件)

using System.Reflection;

using System;

using System.Runtime.InteropServices;

using System.Runtime.ConstrainedExecution;

 

[assembly: AssemblyVersion("4.0.0.0")]

[assembly: CLSCompliant(true)]

[assembly: ComVisible(false)]

[assembly: ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]

 

[assembly: AssemblyTitle(".NET Pet Shop BLL")]

[assembly: AssemblyDescription(".NET Pet Shop Middle-Tier Components")]

[assembly: AssemblyConfiguration("")]

[assembly: AssemblyCompany("Microsoft Corporation")]

[assembly: AssemblyProduct(".NET Pet Shop 4.0")]

[assembly: AssemblyCopyright("Copyright ?2005 Microsoft Corporation")]

[assembly: AssemblyTrademark("")]

[assembly: AssemblyCulture("")]

 

 

例子二:[DataObjectAttribute]

 

例子三:废弃方法,可用于版控制

   using System;
     public class AnyClass
     {
     [Obsolete("Don't use Old method, use New method", true)]
     static void Old( ) { }
     static void New( ) { }
     public static void Main( )
     {
     Old( );
     }
     }

 

Obsolete特性,它标记了一个不应该再被使用的程序实体。第一个参数是一个字符串,它解释了为什么该实体是过时的以及应该用什么实体来代替它。实际上,你可以在这里写任何文本。第二个参数告诉编译器应该把使用这个过时的程序实体当作一种错误。它的默认值是false,也就是说编译器对此会产生一个警告。

 

 

 

例子四:编译器的指令Conditional

 

#define DEBUG

[Conditional("DEBUG")]

public static void Insert(Object obj)

{……}

 

 

五、控件开发中特性的应用

属性 (Property) 和事件的特性 (Attribute)

下表列出了常用于属性 (Property) 和事件的特性 (Attribute)

属性 (Attribute)

应用于

说明

BrowsableAttribute

属性 (Property) 和事件

指定属性 (Property) 或事件是否应该显示在属性 (Property) 浏览器中。

CategoryAttribute

属性 (Property) 和事件

指定类别的名称,在该类别中将对属性 (Property) 或事件进行分组。当使用了类别时,组件属性 (Property) 和事件可以按逻辑分组显示在属性 (Property) 浏览器中。

DescriptionAttribute

属性 (Property) 和事件

定义一小块文本,该文本将在用户选择属性 (Property) 或事件时显示在属性 (Property) 浏览器底部。

BindableAttribute

属性

指定是否要绑定到该属性 (Property)

DefaultPropertyAttribute

属性

(将此属性 (Attribute) 插入类声明前。)

指定组件的默认属性 (Property)。当用户单击控件时,将在属性 (Property) 浏览器中选定该属性 (Property)

DefaultValueAttribute

属性

为属性 (Property) 设置一个简单的默认值。

EditorAttribute

属性

指定在可视设计器中编辑(更改)属性 (Property) 时要使用的编辑器。

LocalizableAttribute

属性

指定属性 (Property) 可本地化。当用户要本地化某个窗体时,任何具有该属性 (Attribute) 的属性 (Property) 都将自动永久驻留到资源文件中。

DesignerSerializationVisibilityAttribute

属性

指定显示在属性 (Property) 浏览器中的属性 (Property) 是否应该(以及如何)永久驻留在代码中。

TypeConverterAttribute

属性

指定将属性 (Property) 的类型转换为另一个数据类型时要使用的类型转换器。

DefaultEventAttribute

事件

(将此属性 (Attribute) 插入类声明前。)

指定组件的默认事件。这是当用户单击组件时在属性 (Property) 浏览器中选定的事件。

除非另外说明,属性 (Property) 和事件的特性 (Attribute) 在代码中紧接在属性 (Property) 或事件声明的前面。

六、自定义特性

a)         定义属性类
public MyCustomAttribute : Attribute { ... }

b)        定义/控制自定义特性的使用
  AttributeUsage 类是另一个预定义的特性类,以帮助我们控制自定义特性的使用。亦即我们可以定义自定义类的特性。这个类描述了如何使用自定义的特性类。有三个数据特性可用以修饰我们的自定义的特性:
 

ValidOn

定义了自定义特性在哪些程序实体上可被使用。这个可使用实体的列表可通过AttributeTargets枚举类型的OR操作进行设置

AllowMultiple

定义了是否可在同一个程序实体上同时使用多个特性进行修饰

Inherited

定义了自定义特性的修饰是否可由被修饰类的派生类继承


AttributeUsage 的作用目标

标记

用途

All

用于任何地方

Assembly

用于组合体

Class

用于类

Contructor

用于构造函数

Delegate

用于委托

Enum

用于枚举

Event

用于事件

Field

用于事件

Interface

用于接口

Method

用于方法

Module

用于模块

Parameter

用于参数

Property

用于属性

Return Value

用于返回值

Struct

用于结构

 

c)        利用反射获取自定义特性
GetCustomAttributes()

d)        自定义特性的应用实例

七、自定义特性在拦截机制上的应用简介

a)         业务逻辑,权限处理等等

b)        例子:订单库存检查

转载于:https://www.cnblogs.com/daniel-zhang/archive/2009/05/26/1489346.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值