123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Reflection;
- using System.Text;
- namespace Ant.ORM
- {
- public static class ReflectionHelper
- {
- private static List<Type> _simpleTypes = new List<Type>
- {
- 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[])
- };
-
- /// <summary>
- ///
- /// </summary>
- /// <param name="lambda"></param>
- /// <returns></returns>
- 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;
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static IDictionary<string, object> GetObjectValues(object obj)
- {
- IDictionary<string, object> result = new Dictionary<string, object>();
- 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;
- }
- /// <summary>
- /// 将列表数据转换字符串用固定符号分隔
- /// </summary>
- /// <param name="list"></param>
- /// <param name="seperator"></param>
- /// <returns></returns>
- public static string AppendStrings(this List<string> list, string seperator = ", ")
- {
- //第一种方法来实行列表转字符串
- //return list.Aggregate(
- // new StringBuilder(),
- // (sb, s) => (sb.Length == 0 ? sb : sb.Append(seperator)).Append(s),
- // sb => sb.ToString());
- return string.Join(seperator, list);
- }
- /// <summary>
- /// 将列表数据转换字符串用固定符号分隔
- /// </summary>
- /// <param name="list"></param>
- /// <param name="seperator"></param>
- /// <returns></returns>
- //public static string AppendStrings(this List<string> list, string seperator = ", ")
- //{
- // return string.Join(seperator, list);
- //}
- /// <summary>
- ///
- /// </summary>
- /// <param name="type"></param>
- /// <returns></returns>
- public static bool IsSimpleType(Type type)
- {
- Type actualType = type;
- if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
- {
- actualType = type.GetGenericArguments()[0];
- }
- return _simpleTypes.Contains(actualType);
- }
- /// <summary>
- /// 获取参数名
- /// </summary>
- /// <param name="parameters"></param>
- /// <param name="parameterName"></param>
- /// <param name="parameterPrefix"></param>
- /// <returns></returns>
- public static string GetParameterName(this QueryCommand parameters, string parameterName)
- {
- return string.Format("{0}_{1}", parameterName, parameters.Parameters.Count);
- }
- public static string SetParameterName(this QueryCommand parameters, string parameterName, object value, FieldValueCollection md)
- {
- string name = parameters.GetParameterName(parameterName);
- parameters.Parameters.Add(parameters.DbParmChar + name, value);
- md.Add(parameters.DbParmChar + name, value);
- return name;
- }
- }
- }
|