示例#1
0
        /// <summary>
        ///  返回一个new表达式(Return a new expression)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="PropertyName"></param>
        /// <returns></returns>
        public static Expression <Func <T, Object> > GetExpression <T>(params string[] PropertyName) where T : class, new()
        {
            List <MemberBinding> Exps      = new List <MemberBinding>();
            ParameterExpression  Parameter = Expression.Parameter(typeof(T), "t");

            PropertyName.ToEach <String>(Item =>
            {
                MemberExpression PropertyExpress = Expression.Property(Parameter, Item);
                UnaryExpression ConvterExpress   = Expression.Convert(PropertyExpress, typeof(T).GetProperty(Item).PropertyType);
                Exps.Add(Expression.Bind(typeof(T).GetProperty(Item), PropertyExpress));
            });
            MemberInitExpression Member = Expression.MemberInit(Expression.New(typeof(T)), Exps);

            return(Expression.Lambda <Func <T, Object> >(Member, Parameter));
        }