C# 通过反射初探ORM框架的实现原理

背景:

  以前学的Java进行开发,多用到Mybatis,Hiberante等ORM框架,最近需要上手一个C#的项目,由于不是特别难,也不想再去学习C#的ORM框架,所以就想着用反射简单的实现一下ORM框架的内容,简单的增删改查,没有用到多表之间的联系。

反射:

  Java和C#中的反射大体相同,主要是指程序可以访问,检测和修改它本身状态或行为的一种能力,并能根据自身行为的状态和结果,调整或修改应用所描述行为的状态和相关的语义。我的理解就是可以程序运行时动态的获取对象的属性和方法,并且可以进行与之相关的调用。

首先看一下C#中反射实现方式:

  获取Type对象,反射操作都需要通过Type对象来进行。

1     通过全限定名来获取  Type tp = Type.GetType("TJCommon.Dao.Deriver");
2 
3     通过类来获取   Type tp = typeof(Int)

  获取到Type对象后我们可以通过其构造方法来创建对象

    调用无参构造

1         // 获取类的初始化构造信息
2         ConstructorInfo ct = tp.GetConstructor(System.Type.EmptyTypes);
3         // 调用不带参数的构造器
4         T newObj = (T)ct.Invoke(null);

    调用有参构造

 1         //定义参数类型数组
 2         Type[] tps = new Type[2];
 3         tps[0] = typeof(int);
 4         tps[1] = typeof(string);
 5         //获取类的初始化参数信息
 6         ConstructorInfo ct2 = tp.GetConstructor(tps);
 7 
 8         //定义参数数组
 9         object[] obj = new object[2];
10         obj[0] = (object)100;
11         obj[1] = (object)"Param Example";
12 
13         //调用带参数的构造器
14         ExampleClass Ex2 = (ExampleClass)ct2.Invoke(obj);

    获得所有公共字段

1         
2         // 获取到所有公共字段
3         FieldInfo[] arr = t.GetFields();     
4 
5         // 给指定的字段赋值   需要传递进来一个对象  newObj
6         f.SetValue(newObj, r[name]);    

 

    这里就介绍这几个方法,通过反射可以获得类中的所有信息,并且可以进行调用,还可以打破封装(不安全) 

练习

  下面就是通过反射将从数据库中获取到的结果集自动封装到Bean中。无需手动封装

 1     public static T dataToObj(String str)
 2         {
 3 
 4             String strSql = str;
 5             DataSet ds = SqlCompose.ExecuteSqlQuery(strSql);
 6 
 7             Type t = typeof(T);
 8             DataRow r = ds.Tables[0].Rows[0];   // 找到一行
 9             FieldInfo[] arr = t.GetFields();   // 返回所有公共字段(public)
10             ConstructorInfo ct = t.GetConstructor(System.Type.EmptyTypes);
11             T newObj = (T)ct.Invoke(null);
12             if (r != null)
13             {
14                 foreach (FieldInfo f in arr)// 遍历所有字段
15                 {
16                     string name = f.Name;
17                     Type type2 = f.FieldType;
18                     if (r[name].GetType() != typeof(DBNull))
19                     {
20                         string typeName = f.FieldType.Name;
21                         f.SetValue(newObj, r[name]);
22                     }
23                 }
24             }
25             else
26             {
27                 newObj = default(T);
28             }
29             ds.Tables.Clear();
30 
31             return newObj;
32         }

 

  封装到List

 1     public static List<T> dataToList(String str)
 2         {
 3             List<T> list = new List<T>();
 4 
 5             String strSql = str;
 6             DataSet ds = SqlCompose.ExecuteSqlQuery(strSql);
 7 
 8             Type t = typeof(T);
 9             FieldInfo[] arr = t.GetFields();   // 返回所有公共字段(public)
10             ConstructorInfo ct = t.GetConstructor(System.Type.EmptyTypes);
11 
12             foreach (DataRow dr in ds.Tables[0].Rows)
13             {
14                 T newObj = (T)ct.Invoke(null);
15                 foreach (FieldInfo f in arr)// 遍历所有字段
16                 {
17                     string name = f.Name;
18                     Type type2 = f.FieldType;
19                     string typeName = f.FieldType.Name;
20                     if (dr[name].GetType() != typeof(DBNull))
21                     {
22                         f.SetValue(newObj, dr[name]);
23                     }
24 
25                 }
26 
27                 list.Add(newObj);
28 
29             }
30             ds.Tables.Clear();
31             return list;
32 
33         }

 

  拼接字符串进行insert操作

 1     public static void inserByBean(string tableName, T target)
 2     {
 3 
 4         StringBuilder sql = new StringBuilder(); // 拼接的sql
 5 
 6         sql.Append("insert into "+tableName+"(");
 7 
 8         Type t = target.GetType();
 9         PropertyInfo[] ps = t.GetProperties();
10 
11         for (int i = 0; i < ps.Length; i++)
12         {
13 
14             object obj = ps[i].GetValue(target, null);
15             if (obj != null)
16             {
17                 string name = ps[i].Name;
18                 if (i != ps.Length - 1)
19                 {
20                     sql.Append(" " + name + ",");
21                 }
22                 else
23                 {
24                     sql.Append(" " + name + "");
25                 }
26             }
27         }
28 
29         sql.Append(") values(");
30 
31 
32         for (int i = 0; i < ps.Length; i++)
33         {
34             object obj = ps[i].GetValue(target, null);
35 
36             if (obj != null)
37             {
38                 if (i != ps.Length - 1)
39                 {
40                     if (ps[i].PropertyType == typeof(string) || ps[i].PropertyType == typeof(DateTime))
41                     {
42                         sql.Append("'" + obj + "',");
43                     }
44                     else {
45                         sql.Append("" + obj + ",");
46                     }
47                 }
48                 else
49                 {
50                     if (ps[i].PropertyType == typeof(string) || ps[i].PropertyType == typeof(DateTime))
51                     {
52                         sql.Append("'" + obj + "')");
53                     }
54                     else
55                     {
56                         sql.Append("" + obj + ")");
57                     }
58                 }
59             }
60         }
61         string resultSql = sql.ToString();
62         SqlCompose.ExecuteSqlNonQuery(resultSql);
63     }

 

posted @ 2017-12-12 20:56  cccc_hi  阅读(3460)  评论(5编辑  收藏  举报