Example #1
0
        /// <summary>
        ///  返回一个new表达式
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="PropertyName"></param>
        /// <returns></returns>
        internal 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.ForArrayEach <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));
        }