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 }