C#利用反射+特性实现简单的实体映射数据库操作类
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 反射_特性 { [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)] public class FieldAttribute : Attribute { private string _Fields; /// <summary> /// 字段名称 /// </summary> public string Fields { get { return _Fields; } } private DbType _Dbtype; /// <summary> /// 字段类型 /// </summary> public DbType Dbtype { get { return _Dbtype; } } private int _ValueLength; /// <summary> /// 字段值长度 /// </summary> public int ValueLength { get { return _ValueLength; } } /// <summary> /// 构造函数 /// </summary> /// <param name="fields"> 字段名</param> /// <param name="types"> 字段类型</param> /// <param name="i"> 字段值长度</param> public FieldAttribute(string fields, DbType types, int i) { _Fields = fields; _Dbtype = types; _ValueLength = i; } } }
2:表名特性
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 反射_特性 { [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)] public class TableAttribute : Attribute { private string _TableName; /// <summary> /// 映射的表名 /// </summary> public string TableName { get { return _TableName; } } /// <summary> /// 定位函数映射表名; /// </summary> /// <param name="table"></param> public TableAttribute(string table) { _TableName = table; } } }
3:特性测试类
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 特性_反射 { [Table("Consumers")] public class UserInf { private string _UserID; /// <summary> /// 登陆ID /// </summary> [Field("ConsumerID", DbType.String, 12)] public string U_UserID { get { return _UserID; } set { _UserID = value; } } private string _Psw; /// <summary> /// 登陆密码 /// </summary> [Field("ConsumerPwd", DbType.String, 12)] public string U_Psw { get { return _Psw; } set { _Psw = value; } } private string _UserName; /// <summary> /// 用户别称 /// </summary> [Field("ConsumerName", DbType.String, 50)] public string U_UserName { get { return _UserName; } set { _UserName = value; } } private string _City; /// <summary> /// 所住城市 /// </summary> [Field("UserCity", DbType.String, 50)] public string U_City { get { return _City; } set { _City = value; } } private int _Popedom; /// <summary> /// 权限 /// </summary> [Field("popedom", DbType.Int32, 0)] public int U_Popedom { get { return _Popedom; } set { _Popedom = value; } } private DateTime _AddDataTime; /// <summary> /// 注册时间 /// </summary> [Field("addDataTime", DbType.Date, 0)] public DateTime U_AddDataTime { get { return _AddDataTime; } set { _AddDataTime = value; } } private int _Sex; /// <summary> /// 性别 /// </summary> [Field("Sex", DbType.Int32, 0)] public int U_Sex { get { return _Sex; } set { _Sex = value; } } private int _BirthTime; /// <summary> /// 出身日期; /// </summary> [Field("BirthTime", DbType.String, 9)] public int U_BirthTime { get { return _BirthTime; } set { _BirthTime = value; } } } }4:测试控制台程序
UserInf userss = new UserInf(); userss.U_UserID = "aw12311"; userss.U_Psw = "123"; userss.U_UserName = "aw"; userss.U_City = "武汉"; userss.U_Popedom = 1; userss.U_Sex = 1; userss.U_BirthTime = 19900114; userss.U_AddDataTime = DateTime.Now; DateIsTableAttribute<UserInf> t = new DateIsTableAttribute<UserInf>(); Response.Write(" </br>" + t.insertDate(userss));