ReflectionHelper.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Reflection;
  6. using System.Text;
  7. namespace Ant.ORM
  8. {
  9. public static class ReflectionHelper
  10. {
  11. private static List<Type> _simpleTypes = new List<Type>
  12. {
  13. typeof(byte),
  14. typeof(sbyte),
  15. typeof(short),
  16. typeof(ushort),
  17. typeof(int),
  18. typeof(uint),
  19. typeof(long),
  20. typeof(ulong),
  21. typeof(float),
  22. typeof(double),
  23. typeof(decimal),
  24. typeof(bool),
  25. typeof(string),
  26. typeof(char),
  27. typeof(Guid),
  28. typeof(DateTime),
  29. typeof(DateTimeOffset),
  30. typeof(byte[])
  31. };
  32. /// <summary>
  33. ///
  34. /// </summary>
  35. /// <param name="lambda"></param>
  36. /// <returns></returns>
  37. public static MemberInfo GetProperty(LambdaExpression lambda)
  38. {
  39. Expression expr = lambda;
  40. for (; ; )
  41. {
  42. switch (expr.NodeType)
  43. {
  44. case ExpressionType.Lambda:
  45. expr = ((LambdaExpression)expr).Body;
  46. break;
  47. case ExpressionType.Convert:
  48. expr = ((UnaryExpression)expr).Operand;
  49. break;
  50. case ExpressionType.MemberAccess:
  51. MemberExpression memberExpression = (MemberExpression)expr;
  52. MemberInfo mi = memberExpression.Member;
  53. return mi;
  54. default:
  55. return null;
  56. }
  57. }
  58. }
  59. /// <summary>
  60. ///
  61. /// </summary>
  62. /// <param name="obj"></param>
  63. /// <returns></returns>
  64. public static IDictionary<string, object> GetObjectValues(object obj)
  65. {
  66. IDictionary<string, object> result = new Dictionary<string, object>();
  67. if (obj == null)
  68. {
  69. return result;
  70. }
  71. foreach (var propertyInfo in obj.GetType().GetProperties())
  72. {
  73. string name = propertyInfo.Name;
  74. object value = propertyInfo.GetValue(obj, null);
  75. result[name] = value;
  76. }
  77. return result;
  78. }
  79. /// <summary>
  80. /// 将列表数据转换字符串用固定符号分隔
  81. /// </summary>
  82. /// <param name="list"></param>
  83. /// <param name="seperator"></param>
  84. /// <returns></returns>
  85. public static string AppendStrings(this List<string> list, string seperator = ", ")
  86. {
  87. //第一种方法来实行列表转字符串
  88. //return list.Aggregate(
  89. // new StringBuilder(),
  90. // (sb, s) => (sb.Length == 0 ? sb : sb.Append(seperator)).Append(s),
  91. // sb => sb.ToString());
  92. return string.Join(seperator, list);
  93. }
  94. /// <summary>
  95. /// 将列表数据转换字符串用固定符号分隔
  96. /// </summary>
  97. /// <param name="list"></param>
  98. /// <param name="seperator"></param>
  99. /// <returns></returns>
  100. //public static string AppendStrings(this List<string> list, string seperator = ", ")
  101. //{
  102. // return string.Join(seperator, list);
  103. //}
  104. /// <summary>
  105. ///
  106. /// </summary>
  107. /// <param name="type"></param>
  108. /// <returns></returns>
  109. public static bool IsSimpleType(Type type)
  110. {
  111. Type actualType = type;
  112. if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
  113. {
  114. actualType = type.GetGenericArguments()[0];
  115. }
  116. return _simpleTypes.Contains(actualType);
  117. }
  118. /// <summary>
  119. /// 获取参数名
  120. /// </summary>
  121. /// <param name="parameters"></param>
  122. /// <param name="parameterName"></param>
  123. /// <param name="parameterPrefix"></param>
  124. /// <returns></returns>
  125. public static string GetParameterName(this QueryCommand parameters, string parameterName)
  126. {
  127. return string.Format("{0}_{1}", parameterName, parameters.Parameters.Count);
  128. }
  129. public static string SetParameterName(this QueryCommand parameters, string parameterName, object value, FieldValueCollection md)
  130. {
  131. string name = parameters.GetParameterName(parameterName);
  132. parameters.Parameters.Add(parameters.DbParmChar + name, value);
  133. md.Add(parameters.DbParmChar + name, value);
  134. return name;
  135. }
  136. }
  137. }