ReflexCls.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Common
  8. {
  9. public class ReflexCls
  10. {
  11. /// <summary>
  12. ///
  13. /// </summary>
  14. /// <param name="objs"></param>
  15. /// <param name="iType">0 Id查询 1非Id查询</param>
  16. /// <param name="strId">iType是0的时候strId则有值</param>
  17. /// <returns></returns>
  18. public static Dictionary<string, object> method(object[] objs,out int iType,out string strId)
  19. {
  20. iType = 1;
  21. strId = "";
  22. object value;
  23. Dictionary<string, object> dt = new Dictionary<string, object>();
  24. foreach (object obj in objs)
  25. {
  26. Type tp = obj.GetType();
  27. if (tp.IsClass)
  28. {
  29. PropertyInfo[] PropertyInfos1 = obj.GetType().GetProperties();
  30. foreach (PropertyInfo p1 in PropertyInfos1)
  31. {
  32. value = p1.GetValue(obj, null);
  33. if (value != null)
  34. {
  35. if (!p1.IsDefined(typeof(AttributeID), false))
  36. {
  37. dt.Add(p1.Name, value);
  38. }
  39. else //带了id的情况
  40. {
  41. if (objs.Length == 1) //如果只有一个条件且条件是主键则能调用根据主键查询的方法,否则调用全字段查询
  42. {
  43. iType = 0;
  44. strId = value + "";
  45. dt.Add(p1.Name, value);
  46. }
  47. else
  48. {
  49. dt.Add(p1.Name, value);
  50. }
  51. }
  52. }
  53. }
  54. }
  55. }
  56. if (dt != null)
  57. {
  58. if (dt.Where(s => s.Key == "pageNum").Count() == 0)
  59. {
  60. dt.Add("pageNum", 1);
  61. }
  62. if (dt.Where(s => s.Key == "pageSize").Count() == 0)
  63. {
  64. dt.Add("pageSize", 999999);
  65. }
  66. }
  67. return dt;
  68. }
  69. }
  70. }