示例#1
0
        private static MemberInfo GetMember <T>(Expression <Func <T, object> > fnMember)
        {
            Guard.NotNull(fnMember, "fnMember");
            var member = Expressor.Member(fnMember);

            Guard.NotNull(member, "member");
            var f = member as FieldInfo;

            if (f != null)
            {
                if (f.IsInitOnly)
                {
                    throw new ArgumentException(string.Format("Field '{0}.{1}' must be can read and write!", typeof(T).Name, f.Name));
                }
                return(f);
            }

            var p = member as PropertyInfo;

            if (p != null)
            {
                if (!p.CanRead || !p.CanWrite)
                {
                    throw new ArgumentException(string.Format("Property '{0}.{1}' must be can read and write!", typeof(T).Name, p.Name));
                }
                return(p);
            }

            throw new ArgumentException(string.Format("'{0}.{1}' must be field or property", typeof(T).Name, member.Name));
        }
示例#2
0
        private Expression BindConcat(Expression[] args)
        {
            if (args.Length == 1)
            {
                var arg = args[0];
                if (arg.NodeType == ExpressionType.NewArrayInit)
                {
                    args = ((NewArrayExpression)arg).Expressions.ToArray();
                }
                else if (arg.NodeType == ExpressionType.MemberAccess)
                {
                    var m = arg as MemberExpression;
                    var c = m.Expression as ConstantExpression;
                    if (c != null && c.Value != null)
                    {
                        var v = m.Member.GetGetter()(c.Value);
                        if (v != null)
                        {
                            if (v is string)
                            {
                                args[0] = Expression.Constant(v, Types.String);
                            }
                            else if (v is IEnumerable)
                            {
                                var result = new List <Expression>();
                                foreach (var item in v as IEnumerable)
                                {
                                    if (item != null)
                                    {
                                        result.Add(Expression.Constant(item.ToString(), Types.String));
                                    }
                                    else
                                    {
                                        result.Add(Expression.Constant(null, Types.String));
                                    }
                                }
                                args = result.ToArray();
                            }
                            else
                            {
                                args[0] = Expression.Constant(v.ToString(), Types.String);
                            }
                        }
                    }
                }
            }
            var length = args.Length;

            for (int i = 0; i < length; i++)
            {
                args[i] = Expressor.ToString(args[i]);
            }
            return(Expression.Call(MethodRepository.Concat, Expression.NewArrayInit(Types.String, args)));
        }
示例#3
0
        private Expression BindJoin(MethodCallExpression m)
        {
            var separator = m.Arguments[0];
            var args      = new List <Expression>();

            if (m.Arguments[1].NodeType == ExpressionType.NewArrayInit)
            {
                var items = ((NewArrayExpression)m.Arguments[1]).Expressions;
                for (int i = 0; i < items.Count; i++)
                {
                    args.Add(Expressor.ToString(items[i]));
                    if (i != items.Count - 1)
                    {
                        args.Add(separator);
                    }
                }
            }
            else if (m.Arguments[1].NodeType == ExpressionType.Constant)
            {
                var c        = (m.Arguments[1] as ConstantExpression);
                var tmpItems = (c.Value as IEnumerable);
                if (Types.IEnumerable.IsAssignableFrom(c.Type) && c.Type != Types.String)
                {
                    if (tmpItems != null)
                    {
                        var items = tmpItems.Cast <object>().ToList();
                        for (int i = 0; i < items.Count; i++)
                        {
                            var value = items[i];
                            if (value != null)
                            {
                                args.Add(Expression.Constant(value.ToString()));
                            }
                            else
                            {
                                args.Add(Expression.Constant(null, Types.String));
                            }
                            if (i != items.Count - 1)
                            {
                                args.Add(separator);
                            }
                        }
                    }

                    if (args.Count == 0)
                    {
                        args.Add(Expression.Constant(null, Types.String));
                    }
                }
            }

            return(Expression.Call(MethodRepository.Concat, Expression.NewArrayInit(Types.String, args.ToArray())));
        }
示例#4
0
 /// <summary>
 /// 通过Lambda 得到Lambda函数内部的成员对象
 /// </summary>
 /// <param name="func"></param>
 /// <returns></returns>
 public static MemberInfo Member(Expression <Func <object> > func)
 {
     return(Expressor.Member(func));
 }
示例#5
0
        protected override Expression VisitBinary(BinaryExpression b)
        {
            b = base.VisitBinary(b) as BinaryExpression;

            switch (b.NodeType)
            {
            case ExpressionType.Add:
            case ExpressionType.AddChecked:
                if (b.Left.Type.Equals(typeof(String)))
                {
                    return(Expression.Call(MethodRepository.Concat, Expression.NewArrayInit(Types.String, b.Left, Expressor.ToString(b.Right))));
                }
                if (b.Right.Type.Equals(typeof(String)))
                {
                    return(Expression.Call(MethodRepository.Concat, Expression.NewArrayInit(Types.String, Expressor.ToString(b.Left), b.Right)));
                }
                return(b);

            case ExpressionType.ArrayIndex:
                if (b.Type == Types.Byte)
                {
                }
                return(b);

            case ExpressionType.Equal:
            case ExpressionType.NotEqual:
            default:
                return(b);
            }
        }
示例#6
0
 public MethodInfo GetGroupingMethodInfo <TKey, TElement>()
 {
     return(Expressor <ExpressionInfo <T> > .MethodExpressor(e => e.GetGrouping <TKey, TElement>(null, null, null, null, null, null)));
 }
示例#7
0
 public MethodInfo GetGroupJoinEnumeratorMethodInfo <TElement>()
 {
     return(Expressor <ExpressionInfo <T> > .MethodExpressor(e => e.GetGroupJoinEnumerator <TElement>(null, null, null, null, 0, null)));
 }
示例#8
0
 public MethodInfo GetMapperMethodInfo()
 {
     return(Expressor <ExpressionInfo <T> > .MethodExpressor(e => e.MapDataReaderToObject(null, null, 0)));
 }