• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Never Give Up
天才在于勤奋,知识在于积累
博客园    首页    新随笔    联系   管理    订阅  订阅

Expression Tree创建任意构造函数参数的方法

class Cat
    {
        public int Year { get; private set; }
        public string Name { get; private set; }

        public Cat()
        { }

        public Cat(int year, string name)
        {
            this.Year = year;
            this.Name = name;
        }

        public override string ToString()
        {
            return string.Format("Name is {0}, Year is {1}", this.Name, this.Year);
        }

    }

    class Test3
    {
        public static void Main(string[] args)
        {
            Cat obj1 = typeof(Cat).CreateInstance<Cat>();
            Cat obj2 = typeof(Cat).CreateInstance<Cat>(2, "Mi Mi");
            Console.WriteLine(obj1.ToString());
            Console.WriteLine(obj2.ToString());
        }
    }

    /// <summary>
    /// Expression Extension Methods
    /// </summary>
    public static class Helper
    {
        /// <summary>
        /// Create object.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="objects"></param>
        /// <returns></returns>
        public static T CreateInstance<T>(this Type type, params object[] objects)
        {
            Type[] typeArray = objects.Select(obj => obj.GetType()).ToArray();
            Func<object[], object> deleObj = BuildDeletgateObj(type, typeArray);
            return (T)deleObj(objects);
        }

        /// <summary>
        /// Get a delegate object and use it to generate a entity class.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="typeList"></param>
        /// <returns></returns>
        private static Func<object[], object> BuildDeletgateObj(Type type, Type[] typeList)
        {
            ConstructorInfo constructor = type.GetConstructor(typeList);
            ParameterExpression paramExp = Expression.Parameter(typeof(object[]), "args_");
            Expression[] expList = GetExpressionArray(typeList, paramExp);

            NewExpression newExp = Expression.New(constructor, expList);

            Expression<Func<object[], object>> expObj = Expression.Lambda<Func<object[], object>>(newExp, paramExp);
            return expObj.Compile();
        }

        /// <summary>
        /// Get an expression array.
        /// </summary>
        /// <param name="typeList"></param>
        /// <param name="paramExp"></param>
        /// <returns></returns>
        private static Expression[] GetExpressionArray(Type[] typeList, ParameterExpression paramExp)
        {
            List<Expression> expList = new List<Expression>();
            for(int i = 0; i < typeList.Length; i++)
            {
                var paramObj = Expression.ArrayIndex(paramExp, Expression.Constant(i));
                var expObj = Expression.Convert(paramObj, typeList[i]);
                expList.Add(expObj);
            }

            return expList.ToArray();
        }
    }

posted @ 2009-12-29 15:21  Terry Sun  阅读(625)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3