



免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C 自定义特性自定义特性 Attribute 类将预定义的系统信息或用户定义的自定义信息与目标元素相关联 目标 元素可以是程序集 类 构造函数 委托 枚举 事件 字段 接口 方法 可移植可执 行文件模块 参数 属性 返回值 结构或其他特性 特性所提供的信息也称为元数据 元数据可由应用程序在运行时进行检查以控制程序 处理数据的方式 也可以由外部工具在运行前检查以控制应用程序处理或维护自身的方式 所有特性类型都直接或间接地从 Attribute 类派生 特性可应用于任何目标元素 多 个特性可应用于同一目标元素 并且特性可由从目标元素派生的元素继承 使用 AttributeTargets 类可以指定特性所应用到的目标元素 以下用一个类似于 Hibernate 中 Session 的 Save 方法效果 自动持久化对象信息到数 据库来说明自定义特性的使用 案例说明 案例说明 1 建立建立Dept表表 Create Table Dept deptNo int identity 1 1 primary key dname nvarchar 10 not null description nvarchar 100 Go 2 自定义特新类自定义特新类 自定义特性类 作用 用来说明表名是什么 AttributeUsage 说明特性的目标元素是什么 AttributeTargets Class 代表目标元素为Class AttributeUsage AttributeTargets Class public class TableAttribute Attribute 表名 public string TableName get set region 构造方法 可选的构造方法 可选的 public TableAttribute public TableAttribute string tableName this TableName tableName endregion 自定义特性类 作用 说明列是否为自动增长列 AttributeUsage AttributeTargets Property class IdentityAttribute Attribute true 是 false 否 public bool IsIdentity get set 实体类 有意将类名定义成与表名不一致 用Table特性来说明实体类对应的表名是什么 Table TableName Dept public class Department 部门编号 用特性标注为自动增长 Identity IsIdentity true public int DeptNo get set 部门名称 public string Dname get set 部门描述 public string Description get set public Department string name string desc Dname name Description desc 执行持久化操作类 执行持久化操作类 public class ADOManager 将对象的属性值作为表中对应列的值来添加 要添加的对象 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 返回执行结果 测试类关键代码 Department dept new Department 开发部 负责产品的研发 ADOManager manager new ADOManager int r manager Save dept Console WriteLine r 0 失败 成功 小结 小结 C 的特性类和 Java 中的元注释一样 特性其本质就是一个继承了 Attribute 的类 使用使可以省略 Att
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026届辽宁省丹东二十九中学英语九上期末监测模拟试题含解析
- 钢质门施工方案范本
- 2026届黑龙江省牡丹江一中学九上化学期中调研试题含解析
- 婴幼儿照料培训
- 2026届浙江省杭州市萧山区北干初级中学英语九上期末经典模拟试题含解析
- 四川省南充市第五中学2026届英语九上期末综合测试试题含解析
- 小学年度工作总结
- 2026届云南省丽江市名校九年级化学第一学期期中质量检测试题含解析
- 2026届吉林省东北师范大附属中学化学九年级第一学期期中学业水平测试试题含解析
- 2026届福建省福州市第十九中学英语九上期末检测模拟试题含解析
- 合肥市肥东县大学生乡村医生专项计划招聘考试真题2024
- 能源问题面试题库及答案
- TSG-T7001-2023电梯监督检验和定期检验规则宣贯解读
- 火电厂工作原理课件
- 重金属在土壤 植物体系中的迁移及其机制课件
- 抢救车管理制度 课件
- 跌倒坠床不良事件鱼骨图分析
- 供应商分级管理制度管理办法
- 招议标管理办法
- 小儿急性上呼吸道感染的护理查房ppt
- 跨文化交际全套课件
评论
0/150
提交评论