特性 Attribute
2024-08-27 07:50:08
特性就是一个类,必须是Attribute的子类
一般以Attribute结尾,然后在使用的时候,可以去掉这个结尾
可以在特性中声明字段、属性、方法、构造函数、委托、事件...
[AttributeUsage (AttributeTargets.All ,AllowMultiple =true )] //这样这个属性可以重复在一个类上使用
class CustomAttribute:Attribute
{
public CustomAttribute ()
{ }
private string _Remark = "";
public CustomAttribute (string remark)
{
this._Remark = remark;
} public string Description { get; set; } public int Id; public void Log()
{
Console.WriteLine(this._Remark);
} public delegate void DoDelegate();
public event DoDelegate DoEvent;
}
[Custom] //调用无参数的构造函数
[CustomAttribute()] //调用无参数的构造函数
[CustomAttribute("这里是我的测试")]//调用有参数的构造函数
[CustomAttribute("这里是Remark的",Description= "这里是Description")]//调用有参构造函数,给属性赋值
[CustomAttribute(Description = "这里是Description")]//调用无参构造函数,给属性赋值
特性可以影响编译,也可以影响运行
特性会被编译到matedata(exe或dll文件包含IL和metadata),metadata数据只能通过反射获取
通过特性,在不侵入原类型的情况,给对象增加了额外的行为
具体看代码
最新文章
- 汇编初入门debug实操
- Linux rm 删除文件
- android开发 NDK 编译和使用静态库、动态库 (转)
- android sqlite 一次创建多个表
- CakePHP的blog教程三
- Android应用程序窗口(Activity)的窗口对象(Window) 的创建过程分析
- 用 Python脚本生成 Android SALT 扰码
- 新版SDK自己主动加入PlaceholderFragment的思考
- 从MSSQL server 2005中移植数据到Oracle 10g
- 你用过CSS3的这个currentColor新属性吗?使用与兼容性
- oracle杀掉连接
- [原创]移动安全测试框架MobSF介绍
- FastCGI点滴
- C# 对图片加水印
- UGUI图集
- 《Android源码设计模式》--原型模式
- ORM------多表操作
- Ghost:一款简约风格博客系统
- springboot之RMI的使用
- Android开发:使用DialogFragment实现dialog自定义布局