using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Text; namespace DapperExtensions { public static class ReflectionHelper { private static List _simpleTypes = new List { typeof(byte), typeof(sbyte), typeof(short), typeof(ushort), typeof(int), typeof(uint), typeof(long), typeof(ulong), typeof(float), typeof(double), typeof(decimal), typeof(bool), typeof(string), typeof(char), typeof(Guid), typeof(DateTime), typeof(DateTimeOffset), typeof(byte[]) }; public static MemberInfo GetProperty(LambdaExpression lambda) { Expression expr = lambda; for (; ; ) { switch (expr.NodeType) { case ExpressionType.Lambda: expr = ((LambdaExpression)expr).Body; break; case ExpressionType.Convert: expr = ((UnaryExpression)expr).Operand; break; case ExpressionType.MemberAccess: MemberExpression memberExpression = (MemberExpression)expr; MemberInfo mi = memberExpression.Member; return mi; default: return null; } } } public static IDictionary GetObjectValues(object obj) { IDictionary result = new Dictionary(); if (obj == null) { return result; } foreach (var propertyInfo in obj.GetType().GetProperties()) { string name = propertyInfo.Name; object value = propertyInfo.GetValue(obj, null); result[name] = value; } return result; } public static string AppendStrings(this IEnumerable list, string seperator = ", ") { return list.Aggregate( new StringBuilder(), (sb, s) => (sb.Length == 0 ? sb : sb.Append(seperator)).Append(s), sb => sb.ToString()); } public static bool IsSimpleType(Type type) { Type actualType = type; if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) { actualType = type.GetGenericArguments()[0]; } return _simpleTypes.Contains(actualType); } public static string GetParameterName(this IDictionary parameters, string parameterName, char parameterPrefix) { return string.Format("{0}{1}_{2}", parameterPrefix, parameterName, parameters.Count); } public static string SetParameterName(this IDictionary parameters, string parameterName, object value, char parameterPrefix) { string name = parameters.GetParameterName(parameterName, parameterPrefix); parameters.Add(name, value); return name; } } }