C#中的特性(Attributes)(翻译)
时间:2010-10-30 来源:superfeeling
翻译不是为了翻译,是为了学习!因为只有翻译我才能逐句的看完整篇文章。
当然还可以得到各位达人的斧正,让我由懂得皮毛到渐入佳境!乐哉,幸哉!
代码
当然还可以得到各位达人的斧正,让我由懂得皮毛到渐入佳境!乐哉,幸哉!
原文
约定:
1.”attribute”和”attributes”均不翻译
2.”property”译为“属性”
3.msdn中的原句不翻译
4.”program entity”译为”语言元素”
Attributes in C#
介绍
Attributes是一种新的描述信息,我们既可以使用attributes来定义设计期信息(例如 帮助文件,文档的URL),还可以用attributes定义运行时信息(例如,使XML中的元素与类的成员字段关联起来)。我们也可以用attributes来创建一个“自描述”的组件。在这篇指南中我们将明白怎么创建属性并将其绑定至各种语言元素上,另外我们怎样在运行时环境下获取到attributes的一些信息。
定义
MSDN 中做如下定义(ms-help://MS.MSDNQTR.2002APR.1033/csspec/html/vclrfcsharpspec_17_2.htm)
"An attribute is a piece of additional declarative information that is specified for a declaration."
使用预定义 Attributes
在c#中已有一小组预定义的attributes,在我们学习怎样创建自定义attributes前,先来了解下在我们的代码中使用那些预定义的attributes.

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( );
}
}
相关阅读 更多 +