C#预定义的Attribute
AttributeUsage:
格式为[AttributeUsage(AttributeTargets , (bool) AllowMultiple ,(boll) Inherited )]
放在继承Attribute的类前面,参数AttributeTargets为Attribute的使用范围;
Attribute的适用范围
/*
All:类
Assembly:程序集
Class:类
Constructor:构造函数
Delegate:委托
Enum:枚举
Event:事件
Field:字段
GenericParameter:泛型参数
Interface:接口
Method:方法
Module:模块
Parameter:参数
Property:属性
ReturnValue:返回值
Struct:结构
*/
(可选)AllowMultiple参数为该特性是否可以多用,默认为false;
(可选)Inherited参数为该特性是否可以被派生类继承,默认为false
Conditional:条件特性
格式为 [Conditional("String")]:String为任意字符
放在方法的前面,在此情况下要调用方法,在类之前要添加预处理标识符
#define String :String为对应的条件特性里的字符串
相当于
#if String
void MyFunction () {
}
#endif
Obsolete:过时的,不应被使用的标记
格式为[Obsolete ("调用此方法时提示的文字", ( bool )iserror ) ]
放在方法的前面,在调用此方法时系统给予Obsolete中的文字提示;可选参数iserror为false时,编辑器生成警告提示,方法仍可以使用,为true时,编辑器提示错误,此方法不可用,(默认为false)