zoukankan      html  css  js  c++  java
  • 自定义特性使用

    元数据,就是C#中封装的一些类,无法修改.类成员的特性被称为元数据中的注释.

    1、什么是特性

         (1)属性与特性的区别

             属性(Property):属性是面向对象思想里所说的封装在类里面的数据字段,Get,Set方法。

             特性(Attribute):  官方解释:特性是给指定的某一声明的一则附加的声明性信息。 允许类似关键字的描述声明。它对程序中的元素进行标注,如类型、字段、方法、属性等。从.net角度看,特性是一种 类,这些类继承于System.Attribute类,用于对类、属性、方法、事件等进行描述,主要用在反射中。但从面向对象的级别看,其实Attribute是类型级别的,而不是对象级别。

             Attributes和.net文件的元素据保存在一起,可以用来向运行时描述你的代码,或者在程序运行的时候影响程序的行为。

    2、特性的应用

        (1).net中特性用来处理多种问题,比如序列化、程序的安全特性、防止即时编译器对程序代码进行优化从而代码容易调试等等。

         定植特性的本质上是一个类的元素上去添加附加信息,并在运行其通过反射得到该附加信息(在使用数据实体对象时经常用到)

        (2)Attribute 作为编译器的指令时的应用

             Conditional:起条件编译的作用,只有满足条件,才允许编译器对它的代码进行编译。一般在程序调试的时候使用

             DllImport: 用来标记费.net的函数,表明该方法在一个外部的DLL中定义。

             Obsolete: 这个属性用来标记当前的方法已经废弃,不再使用

           注:Attribute是一个类,因此DllImport也是一个类,Attribute类是在编译的时候实例化,而不是像通常那样在运行时实例化。

             CLSCompliant: 保证整个程序集代码遵守CLS,否则编译将报错。

     3、自定义特性

          使用AttributeUsage,来控制如何应用新定义的特性

         [AttributeUsageAttribute(AttributeTargets.All  可以应用到任何元素

          ,AllowMultiple=true, 允许应用多次,我们的定值特性能否被重复放在同一个程序实体前多次。

          ,Inherited=false,不继承到派生

            )]

          特性也是一个类,必须继承于System.Attribute类,命名规范为“类名”+Attribute。不管是直接还是间接继承,都会成为一个特性类,特性类的声明定义了一种可以放置在声明之上新的特性。

         public class MyselfAttribute:System.Attribute

    4、自定义特性案例
         以下用一个类似于Hibernate中Session的Save()方法效果,自动持久化对象信息到数据库来说明自定义特性的使用。

    (1)建立Dept表

    复制代码
    Create Table Dept(  
        deptNo int identity(1,1) primary key,  
        dname nvarchar(10) not null,  
        description nvarchar(100)  
    )  
    Go  
    -- 何问起 hovertree.com 
    复制代码

    (2)自定义特性

    复制代码
    /**************自定义特性类*****************/  
    /// <summary>  
    /// 作用:用来说明表名是什么  
    /// AttributeUsage:说明特性的目标元素是什么  
    /// AttributeTargets.Class:代表目标元素为Class  
    /// </summary>  
    [AttributeUsage(AttributeTargets.Class)]  
    public class TableAttribute : Attribute{  
      
        /// <summary>  
        /// 表名  
        /// </summary>  
        public string TableName { get; set; }  
     
        #region 构造方法,可选的  
      
        public TableAttribute() {    }  
        public TableAttribute(string tableName) {  
            this.TableName = tableName;  
        }   
     
        #endregion  
    }  
      
    /**************自定义特性类*****************/  
    /// <summary>  
    /// 作用:说明列是否为自动增长列  
    /// </summary>  
    [AttributeUsage(AttributeTargets.Property)]  
    class IdentityAttribute: Attribute  
    {  
        /// <summary>  
        /// true:是; false:否  
        /// </summary>  
        public bool IsIdentity { get; set; }  
    }  
      
    /****************实体类***************/  
    /// <summary>  
    /// 有意将类名定义成与表名不一致  
    /// 用Table特性来说明实体类对应的表名是什么  
    /// </summary>  
    [Table(TableName = "Dept")]  
    public class Department {  
      
        /// <summary>  
        /// 部门编号,用特性标注为自动增长  
        /// </summary>  
        [Identity(IsIdentity=true)]  
        public int DeptNo { get; set; }  
      
        /// <summary>  
        /// 部门名称  
        /// </summary>  
        public string Dname { get; set; }  
      
        /// <summary>  
        /// 部门描述  
        /// </summary>  
        public string Description { get; set; }  
      
        public Department( string name, string desc) {  
            Dname = name;  
            Description = desc;  
        }  
    }  
      
    /****************执行持久化操作类***************/  
    /// <summary>  
    /// 执行持久化操作类  
    /// </summary>  
    public class ADOManager {  
      
        /// <summary>  
        /// 将对象的属性值作为表中对应列的值来添加  
        /// </summary>  
        /// <param name="obj">要添加的对象</param>  
        public int Save(Object obj) {  
            //1.取得类名:代表表名,用到反射  
            string tableName = obj.GetType().Name;  
            //如果类有TableAttribute特性,在采用特性说明的类名  
            TableAttribute attr = Attribute.GetCustomAttribute(obj.GetType(), typeof(TableAttribute)) as TableAttribute;  
            if (attr != null) {//说明类加了Table特性  
                tableName = attr.TableName;//取得表名  
            }  
      
            //sql语句模板:insert into Dept(deptno,dname,description) values('2','','');  
            StringBuilder sql = new StringBuilder("insert into ");  
            sql.Append(tableName);  
            sql.Append(" (");  
      
            //循环对象的属性名:取得列名  
            foreach (PropertyInfo item in obj.GetType().GetProperties()) {  
                //取得是否有自动增长的特性  
                IdentityAttribute att = Attribute.GetCustomAttribute(item, typeof(IdentityAttribute)) as IdentityAttribute;  
                if (att == null || !att.IsIdentity) {//没有,则添加列  
                    sql.Append(item.Name);  
                    sql.Append(",");  
                }  
            }  
            //去除最后一个逗号'  
            sql.Remove(sql.Length - 1, 1);  
            sql.Append(") values(");  
            //循环取出对象的属性值:为列赋值  
            foreach (PropertyInfo item in obj.GetType().GetProperties()) {  
                //取得是否有自动增长的特性  
                IdentityAttribute att = Attribute.GetCustomAttribute(item, typeof(IdentityAttribute)) as IdentityAttribute;  
                if (att == null) {//没有,则追加列的值  
                    //GetValue():obj代表什么对象,null代表没有参数  
                    sql.Append("'" + item.GetValue(obj, null) + "'");  
                    sql.Append(",");  
                }  
            }  
      
            //去除最后一个逗号'  
            sql.Remove(sql.Length - 1, 1);  
            sql.Append(")");  
            //查看完整的sql语句  
            Console.WriteLine(sql.ToString());  
      
            //执行sql语句  
            SqlConnection conn = new SqlConnection("server=.;database=test;integrated security=true");  
            SqlCommand comm = new SqlCommand(sql.ToString(), conn);  
            conn.Open();  
            int r = comm.ExecuteNonQuery();  
            conn.Close();  
      
            return r;//返回执行结果  
        }  
    }  
      /* 何问起 hovertree.com */
    /****************测试类关键代码***************/  
    Department dept = new Department("开发部", "负责产品的研发");  
    ADOManager manager = new ADOManager();  
    int r = manager.Save(dept);  
      
    Console.WriteLine(r==0?"失败":"成功");  
    复制代码

    小结:

    C#的特性类和Java中的元注释一样

    特性其本质就是一个继承了Attribute的类

    使用使可以省略Attribute结尾,如:TableAttribute =>> Table

    特性将会影响其作用的目标元素的编译和运行过程

    使用自定义特性的步骤:

    1. 定义特性类,类必须直接或间接继承字Attribute类

    2. 在需要用的该特性的目标元素上添加特性

    3. 在使用添加了特性的类的使用,获取并使用自特定特性的信息

    推荐:http://www.cnblogs.com/roucheng/p/dushubiji.html

    处:https://www.cnblogs.com/roucheng/p/cstexing.html
  • 相关阅读:
    Zabbix 配置笔记
    Centos7安装MySQL5.7和Redis6.0流水账
    Elasticsearch, Kibana安装
    服务治理 Service Mesh & Kubernetes & Spring Cloud的异同 侵入式 非侵入式
    文思海辉 华为 银行 ODS (oCRM)
    Java Profiler JavaMemoryLeak Arthas
    动物:人类、熊类、马类 团队精神 与 鼓励
    BPMN Sketch Miner https://design.inf.usi.ch/bpmn-sketch-miner/#
    Architecture, Design and Web Information Systems Engineering
    Cloud design patterns
  • 原文地址:https://www.cnblogs.com/Justsoso-WYH/p/8566402.html
Copyright © 2011-2022 走看看