附上源代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Data; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace 反射_特性 9 {10 [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]11 public class FieldAttribute : Attribute12 {13 private string _Fields;14 ///15 /// 字段名称 keleyi.com16 /// 17 public string Fields18 {19 get { return _Fields; }20 21 }22 23 private DbType _Dbtype;24 ///25 /// 字段类型26 /// 27 public DbType Dbtype28 {29 get { return _Dbtype; }30 31 }32 33 private int _ValueLength;34 ///35 /// 字段值长度36 /// 37 public int ValueLength38 {39 get { return _ValueLength; }40 41 }42 ///43 /// 构造函数44 /// 45 /// 字段名46 /// 字段类型47 /// 字段值长度48 public FieldAttribute(string fields, DbType types, int i)49 {50 51 _Fields = fields;52 _Dbtype = types;53 _ValueLength = i;54 }55 }56 }
2:表名特性
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 反射_特性 8 { 9 [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]10 public class TableAttribute : Attribute11 {12 private string _TableName;13 ///14 /// 映射的表名15 /// 16 public string TableName17 {18 get { return _TableName; }19 }20 ///21 /// 定位函数映射表名;22 /// 23 /// 24 public TableAttribute(string table)25 {26 _TableName = table;27 }28 }29 }
3:特性测试类
1 using System; 2 using System.Collections.Generic; 3 using System.Data; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace 特性_反射 9 { 10 [Table("Consumers")] 11 public class UserInf 12 { 13 private string _UserID; 14 ///15 /// 登陆ID 16 /// 17 [Field("ConsumerID", DbType.String, 12)] 18 public string U_UserID 19 { 20 get { return _UserID; } 21 set { _UserID = value; } 22 } 23 24 private string _Psw; 25 ///26 /// 登陆密码 27 /// 28 [Field("ConsumerPwd", DbType.String, 12)] 29 public string U_Psw 30 { 31 get { return _Psw; } 32 set { _Psw = value; } 33 } 34 35 private string _UserName; 36 ///37 /// 用户别称 38 /// 39 [Field("ConsumerName", DbType.String, 50)] 40 public string U_UserName 41 { 42 get { return _UserName; } 43 set { _UserName = value; } 44 } 45 46 private string _City; 47 ///48 /// 所住城市 49 /// 50 [Field("UserCity", DbType.String, 50)] 51 public string U_City 52 { 53 get { return _City; } 54 set { _City = value; } 55 } 56 57 private int _Popedom; 58 ///59 /// 权限 60 /// 61 [Field("popedom", DbType.Int32, 0)] 62 public int U_Popedom 63 { 64 get { return _Popedom; } 65 set { _Popedom = value; } 66 } 67 68 private DateTime _AddDataTime; 69 ///70 /// 注册时间 71 /// 72 [Field("addDataTime", DbType.Date, 0)] 73 public DateTime U_AddDataTime 74 { 75 get { return _AddDataTime; } 76 set { _AddDataTime = value; } 77 } 78 79 private int _Sex; 80 ///81 /// 性别 82 /// 83 [Field("Sex", DbType.Int32, 0)] 84 public int U_Sex 85 { 86 get { return _Sex; } 87 set { _Sex = value; } 88 } 89 90 private int _BirthTime; 91 ///92 /// 出身日期; 93 /// 94 [Field("BirthTime", DbType.String, 9)] 95 public int U_BirthTime 96 { 97 get { return _BirthTime; } 98 set { _BirthTime = value; } 99 }100 }101 }
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; DateIsTableAttributet = new DateIsTableAttribute (); Response.Write(" " + t.insertDate(userss));