C#比較兩個由基本數據類型構成的object類型

 1    /// <summary>
 2     /// 比較查詢條件
 3     /// </summary>
 4     public class ModelExtensions
 5     {
 6         /// <summary>
 7         /// 擴展方法(用於比較兩個類型)
 8         /// </summary>
 9         /// <param name="modelA"></param>
10         /// <param name="modelB"></param>
11         /// <returns></returns>
12         public bool IsEquals<T>(T objA, T objB)
13         {
14             Type objType = typeof(T);
15             PropertyInfo[] ppts = objType.GetProperties();
16 
17             for (int i = 0; i < ppts.Length; i++)
18             {
19                 object proValueA = null;
20                 object proValueB = null;
21                 proValueA = ppts[i].GetValue(objA, null);
22                 proValueB = ppts[i].GetValue(objB, null);
23 
24                 if (proValueA != null)//可以理解爲沒有賦值的不進行比較
25                 {
26                     if (proValueA.GetType() == typeof(string))//如果是字符型直接比較
27                     {
28                         string int1, int2;
29                         int1 = (string)proValueA;
30                         int2 = (string)proValueB;
31                         if (proValueA != proValueB)
32                         {
33                             return false;
34                         }
35                     }
36                     else if (proValueA.GetType() == typeof(int))//如果是數字型直接比較
37                     {
38                         int int1, int2;
39                         int1 = (int)proValueA;
40                         int2 = (int)proValueB;
41                         if (int1 != int2)
42                         {
43                             return false;
44                         }
45                     }
46                     else if (proValueA.GetType() == typeof(DateTime))//如果是時間型直接比較
47                     {
48                         DateTime int1, int2;
49                         int1 = (DateTime)proValueA;
50                         int2 = (DateTime)proValueB;
51                         if (int1 != int2)
52                         {
53                             return false;
54 
55                         }
56                     }
57                     else if (proValueA.GetType() == typeof(byte))//如果是字節直接比較
58                     {
59                         byte int1, int2;
60                         int1 = (byte)proValueA;
61                         int2 = (byte)proValueB;
62                         if (int1 != int2)
63                         {
64                             return false;
65                         }
66                     }
67                     else if (proValueA.GetType() == typeof(bool))//如果是BOOL直接比較
68                     {
69                         bool int1, int2;
70                         int1 = (bool)proValueA;
71                         int2 = (bool)proValueB;
72                         if (int1 != int2)
73                         {
74                             return false;
75                         }
76                     }
77                     else//其他類型不比較
78                     {
79                         continue;
80                     }
81 
82                 }
83             }
84 
85             return true;
86         }
87     }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章