C# 經過反射初探ORM框架的實現原理

背景:

  之前學的Java進行開發,多用到Mybatis,Hiberante等ORM框架,最近須要上手一個C#的項目,因爲不是特別難,也不想再去學習C#的ORM框架,因此就想着用反射簡單的實現一下ORM框架的內容,簡單的增刪改查,沒有用到多表之間的聯繫。sql

反射:

  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     }
相關文章
相關標籤/搜索