/// <summary>
        ///     Returns delegate that creates an instance of a given type with the packaged set of constructor arguments passed as
        ///     constructorArgs parameters.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="constructorArgs"></param>
        /// <returns></returns>
        public static Func <T> GetFastActivatorWithEmbeddedArgs <T>(params object[] constructorArgs)
        {
            FastObjectActivator rawActivator = GetFastActivator(typeof(T), constructorArgs.ArgsToArgTypes());

            if (rawActivator == null)
            {
                return(null);
            }

            Func <T> activator = () => (T)rawActivator(constructorArgs);

            return(activator);
        }
        /// <summary>
        ///     Returns delegate that creates an instance of a given type with the packaged set of constructor arguments passed as
        ///     constructorArgs parameters.
        /// </summary>
        /// <param name="classType"></param>
        /// <param name="constructorArgs"></param>
        /// <returns>Delegate with parameters.</returns>
        public static Func <object> GetFastActivatorWithEmbeddedArgs(this Type classType, params object[] constructorArgs)
        {
            FastObjectActivator rawActivator = GetFastActivator(classType, constructorArgs.ArgsToArgTypes());

            if (rawActivator == null)
            {
                return(null);
            }

            Func <object> activator = () => rawActivator(constructorArgs);

            return(activator);
        }
        /// <summary>
        ///     Returns delegate of a constructor for a given type, which runs much faster than Reflection-based
        ///     Activator.CreateInstance().
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="constructorArgTypes"></param>
        /// <returns></returns>
        public static Func <object[], T> GetFastActivator <T>(params Type[] constructorArgTypes)
        {
            FastObjectActivator rawActivator = typeof(T).GetFastActivator(constructorArgTypes);

            if (rawActivator == null)
            {
                return(null);
            }

            Func <object[], T> activator = parms => (T)rawActivator(typeof(T), parms);

            return(activator);
        }
        /// <summary>
        ///     Improved version of the activator compiler glanced at
        ///     http://rogeralsing.com/2008/02/28/linq-expressions-creating-objects/
        /// </summary>
        /// <param name="ctorInfo"></param>
        /// <returns></returns>
        private static FastObjectActivator CompileActivator(this ConstructorInfo ctorInfo)
        {
            if (ctorInfo == null)
            {
                throw new ArgumentNullException("ctorInfo");
            }

            ParameterInfo[] paramsInfo = ctorInfo.GetParameters();

            //create a single param of type object[]
            ParameterExpression constructorArgsExp = Expression.Parameter(typeof(object[]), "args");

            Expression[] argsExp = new Expression[paramsInfo.Length];

            //pick each arg from the params array
            //and create a typed expression of them
            for (int i = 0; i < paramsInfo.Length; i++)
            {
                Expression index     = Expression.Constant(i);
                Type       paramType = paramsInfo[i].ParameterType;

                Expression      paramAccessorExp = Expression.ArrayIndex(constructorArgsExp, index);
                UnaryExpression paramCastExp     = Expression.Convert(paramAccessorExp, paramType);

                argsExp[i] = paramCastExp;
            }

            //make a NewExpression that calls the
            //ctor with the args we just created
            NewExpression newExp = Expression.New(ctorInfo, argsExp);

            //create a lambda with the New
            //Expression as body and our param object[] as arg
            Expression <FastObjectActivator> lambda = Expression.Lambda <FastObjectActivator>(newExp, constructorArgsExp);
            FastObjectActivator constructorDelegate = lambda.Compile();

            return(constructorDelegate);
        }