背景:
以前学的Java进行开发,多用到Mybatis,Hiberante等ORM框架,最近需要上手一个C#的项目,由于不是特别难,也不想再去学习C#的ORM框架,所以就想着用反射简单的实现一下ORM框架的内容,简单的增删改查,没有用到多表之间的联系。
反射:
Java和C#中的反射大体相同,主要是指程序可以访问,检测和修改它本身状态或行为的一种能力,并能根据自身行为的状态和结果,调整或修改应用所描述行为的状态和相关的语义。我的理解就是可以程序运行时动态的获取对象的属性和方法,并且可以进行与之相关的调用。
首先看一下C#中反射实现方式:
获取Type对象,反射操作都需要通过Type对象来进行。
- 通过全限定名来获取Type tp = Type.GetType("TJCommon.Dao.Deriver");
- 通过类来获取Type tp = typeof(Int)
获取到Type对象后我们可以通过其构造方法来创建对象
调用无参构造
- // 获取类的初始化构造信息
- ConstructorInfo ct = tp.GetConstructor(System.Type.EmptyTypes);
- // 调用不带参数的构造器
- T newObj = (T) ct.Invoke(null);
调用有参构造
- //定义参数类型数组
- Type[] tps = new Type[2];
- tps[0] = typeof(int);
- tps[1] = typeof(string);
- //获取类的初始化参数信息
- ConstructorInfo ct2 = tp.GetConstructor(tps);
- //定义参数数组
- object[] obj = new object[2];
- obj[0] = (object) 100;
- obj[1] = (object)"Param Example";
- //调用带参数的构造器
- ExampleClass Ex2 = (ExampleClass) ct2.Invoke(obj);
获得所有公共字段
- // 获取到所有公共字段
- FieldInfo[] arr = t.GetFields();
- // 给指定的字段赋值 需要传递进来一个对象 newObj
- f.SetValue(newObj, r[name]);
这里就介绍这几个方法,通过反射可以获得类中的所有信息,并且可以进行调用,还可以打破封装(不安全)
练习
下面就是通过反射将从数据库中获取到的结果集自动封装到Bean中。无需手动封装
- public static T dataToObj(String str)
- {
- String strSql = str;
- DataSet ds = SqlCompose.ExecuteSqlQuery(strSql);
- Type t = typeof(T);
- DataRow r = ds.Tables[0].Rows[0]; // 找到一行
- FieldInfo[] arr = t.GetFields(); // 返回所有公共字段(public)
- ConstructorInfo ct = t.GetConstructor(System.Type.EmptyTypes);
- T newObj = (T)ct.Invoke(null);
- if (r != null)
- {
- foreach (FieldInfo f in arr)// 遍历所有字段
- {
- string name = f.Name;
- Type type2 = f.FieldType;
- if (r[name].GetType() != typeof(DBNull))
- {
- string typeName = f.FieldType.Name;
- f.SetValue(newObj, r[name]);
- }
- }
- }
- else
- {
- newObj = default(T);
- }
- ds.Tables.Clear();
- return newObj;
- }
封装到List
- public static List<T> dataToList(String str)
- {
- List<T> list = new List<T>();
- String strSql = str;
- DataSet ds = SqlCompose.ExecuteSqlQuery(strSql);
- Type t = typeof(T);
- FieldInfo[] arr = t.GetFields(); // 返回所有公共字段(public)
- ConstructorInfo ct = t.GetConstructor(System.Type.EmptyTypes);
- foreach (DataRow dr in ds.Tables[0].Rows)
- {
- T newObj = (T)ct.Invoke(null);
- foreach (FieldInfo f in arr)// 遍历所有字段
- {
- string name = f.Name;
- Type type2 = f.FieldType;
- string typeName = f.FieldType.Name;
- if (dr[name].GetType() != typeof(DBNull))
- {
- f.SetValue(newObj, dr[name]);
- }
- }
- list.Add(newObj);
- }
- ds.Tables.Clear();
- return list;
- }
拼接字符串进行insert操作
- public static void inserByBean(string tableName, T target)
- {
- StringBuilder sql = new StringBuilder(); // 拼接的sql
- sql.Append("insert into "+tableName+"(");
- Type t = target.GetType();
- PropertyInfo[] ps = t.GetProperties();
- for (int i = 0; i < ps.Length; i++)
- {
- object obj = ps[i].GetValue(target, null);
- if (obj != null)
- {
- string name = ps[i].Name;
- if (i != ps.Length - 1)
- {
- sql.Append(" " + name + ",");
- }
- else
- {
- sql.Append(" " + name + "");
- }
- }
- }
- sql.Append(") values(");
- for (int i = 0; i < ps.Length; i++)
- {
- object obj = ps[i].GetValue(target, null);
- if (obj != null)
- {
- if (i != ps.Length - 1)
- {
- if (ps[i].PropertyType == typeof(string) || ps[i].PropertyType == typeof(DateTime))
- {
- sql.Append("'" + obj + "',");
- }
- else {
- sql.Append("" + obj + ",");
- }
- }
- else
- {
- if (ps[i].PropertyType == typeof(string) || ps[i].PropertyType == typeof(DateTime))
- {
- sql.Append("'" + obj + "')");
- }
- else
- {
- sql.Append("" + obj + ")");
- }
- }
- }
- }
- string resultSql = sql.ToString();
- SqlCompose.ExecuteSqlNonQuery(resultSql);
- }