Esempio n. 1
0
        public static PropResult GetExpressionValue <TSource, TProperty>(this TSource source,
                                                                         Expression <Func <TSource, TProperty> > exp)
        {
            Type type = typeof(TSource);

            if (exp.Body is MethodCallExpression)
            {
                var member       = exp.Body as MethodCallExpression;
                var methodResult = Expression.Lambda(member, exp.Parameters.ToArray()).Compile().DynamicInvoke(source);

                var result = new PropResult();

                result.Name       = member.ToString();
                result.Value      = methodResult;
                result.ObjectType = source.GetType();

                return(result);
            }

            if (exp.Body is UnaryExpression)
            {
                var member       = exp.Body as UnaryExpression;
                var methodResult = Expression.Lambda(member, exp.Parameters.ToArray()).Compile().DynamicInvoke(source);

                var result = new PropResult();

                result.Name       = member.ToString();
                result.Value      = methodResult;
                result.ObjectType = source.GetType();

                return(result);
            }

            if (exp.Body is MemberExpression)
            {
                var member   = exp.Body as MemberExpression;
                var propInfo = member.Member as PropertyInfo;

                var result = new PropResult();

                result.Name       = propInfo.Name;
                result.Value      = propInfo.GetValue(source, null);
                result.ObjectType = source.GetType();

                return(result);
            }

            throw new NotImplementedException("GetExpressionValue");
        }
Esempio n. 2
0
        public static PropResult GetExpressionValue <TSource, TProperty>(this TSource source,
                                                                         Expression <Func <TSource, TProperty> > exp)
        {
            Type type = typeof(TSource);

            if (exp.Body is MethodCallExpression)
            {
                var member = exp.Body as MethodCallExpression;

                if (!_cache4GetExpressionValueAsMethodCallExpression.ContainsKey(exp))
                {
                    var compiledLambda = Expression.Lambda(member, exp.Parameters.ToArray()).Compile();
                    _cache4GetExpressionValueAsMethodCallExpression.Add(exp, compiledLambda);
                }

                //var methodResult = compiledLambda.DynamicInvoke(source);
                var methodResult = _cache4GetExpressionValueAsMethodCallExpression[exp].DynamicInvoke(source);

                var result = new PropResult();

                result.Name       = member.ToString();
                result.Value      = methodResult;
                result.ObjectType = source.GetType();

                return(result);
            }

            if (exp.Body is UnaryExpression)
            {
                var member = exp.Body as UnaryExpression;

                if (!_cache4GetExpressionValueAsUnaryExpression.ContainsKey(exp))
                {
                    var compiledLambda = Expression.Lambda(member, exp.Parameters.ToArray()).Compile();
                    _cache4GetExpressionValueAsUnaryExpression.Add(exp, compiledLambda);
                }

                //var methodResult = compiledLambda.DynamicInvoke(source);
                var methodResult = _cache4GetExpressionValueAsUnaryExpression[exp].DynamicInvoke(source);

                var result = new PropResult();

                result.Name       = member.ToString();
                result.Value      = methodResult;
                result.ObjectType = source.GetType();

                return(result);
            }

            if (exp.Body is MemberExpression)
            {
                var member   = exp.Body as MemberExpression;
                var propInfo = member.Member as PropertyInfo;

                var result = new PropResult();

                result.Name = propInfo.Name;

                if (!_cache4FastGetters.ContainsKey(type))
                {
                    _cache4FastGetters.Add(type, new Dictionary <PropertyInfo, Delegate>());
                }

                var _cache4FastGettersFunctions = _cache4FastGetters[type];

                if (!_cache4FastGettersFunctions.ContainsKey(propInfo))
                {
                    var fastGetter = BuildUntypedGetter <TSource>(propInfo);
                    _cache4FastGettersFunctions.Add(propInfo, fastGetter);
                }
                else
                {
                }

                var value = _cache4FastGettersFunctions[propInfo].DynamicInvoke(source);
                //result.Value = propInfo.GetValue(source, null);
                result.Value = value;

                result.ObjectType = source.GetType();

                return(result);
            }

            throw new NotImplementedException("GetExpressionValue");
        }