当前位置: 移动技术网 > IT编程>开发语言>.net > .Net 特性 attribute 学习 ----自定义特性

.Net 特性 attribute 学习 ----自定义特性

2019年02月14日  | 移动技术网IT编程  | 我要评论

再续大明时,诚信公棚,苏泊尔电压力锅售后

什么是特性?
[obsolete("不要用无参构造函数",true)] 放在方式上, 该方法就不能使用了
 [serializable]放在类上面。该类就是可以序列化和反序列化使用了。
 
在命名空间、类、方法、属性、字段、枚举 上用中括号[]
 
自定义特性,特性就是类:必须继承attribute 或者是attribute的泛生类
public class sizeattribute : attribute       // 这个就是一个自定义特性
{
          public sizeattribute()
          {
                   console.writeline("这是一个sizeattribute的构造函数");
           }
 
这个特性就创建好了
在其他类, 如student类上
[sizeattribute]        //在类上写特性
public class student
{
         [sizeattribute] //在属性上写特性
          public int id{set; get;}
 
          public string name{set;get}
 
           [sizeattribute] //在方法上写特性
           public void show()
           {
                   console.writeline("show")
             }
}
 
当然特性 也可以有描述自己特性的办法
就是在特性上面写上
[attributeusage(attributetargets.all,allowmultiple =false,inherited =true)]
public class sizeattribute : attribute
{
}
 
//意思是当前特性包含所有类型都可以使用,只能单一使用,可以继承
 
 
特性:
1.当程序编译和执行,特性和注释的效果是一样的,没有任何不同
 
2.特性编译后是metadata,只有在反射的时候,才能使用特性。
 
3.特性可以做权限检测,属性验证,封装枚举等很多功能。
 
4.特性是一个类,可以用作标记元素,编译时生成在metadata里,平时不影响程序的运行,除非主动用反射去查找,
可以得到一些额外的信息和操作,提供了更丰富扩展空间,特性可以在不 破坏类型封装的前提下,额外增加功能。
 
 
例子:有一个学生类,希望用特性,让添加的学生年龄不能小于12岁,大于20岁
 
//学生类
public class student
{
            public int id { get; set; }

            public string name { get; set; }

             public int age { get; set; }

            [obsolete("不要用无参构造函数",true)] //这个特性,是不能使用无参构造函数
            public student()
            { }

            public student(int id, string name,int age)
           {
            this.id = id;
            this.name = name;
 
            [controlageattribute(_vmin=12,_vmax=30)] //要判断年龄,年龄小于20,大于12, 就将下面自定义的特性放在这个属性上面
            this.age = age;
            }

            public void show()
            {
              console.writeline("这个show方法");
             }
}
 
//控制年龄的特性 :特性的命名规范--名称后面为attribute
public class controlageattribute : attribute
{
          public int _vmin{get;set;}//最小年龄 
           public int _vmax{get;set;} //最大年龄        
 
          public bool  compareage(int age)
          {
                   return age>_vmin && age <_vmax ? true : false; //
           }
}
 
//反射使用特性---用静态方法
public static class manage
{
          public static bool compareagemanage(this student stu)
         {
               bool result = false;
               type type = typeof(stu);//先获取类型
                properyinfo prop = type.getproperty("age");//反射获取年龄属性
                if (prop.isdefined(typeof(controlageattribute ),true))//判断当前属性是否有controlageattribute 的特性
                {
                      controlageattribute  attribute = (controlageattribute) prop.getcustomattribute(typeof(controlageattribute ),true);
                        //获取特性
                      result =  attribute.compareage(stu.age);
                      return result;//得到结果返回
                 }
                 
                        
 
              return result;
         }
}
 
//控制台main方法里面执行
static void main(string[] args)
{
student student = new student(12,"hahaha",15);
console.writeline(student.compareagemanage());  //15在12和20 之间,所以是true;


}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网