Example #1
0
        private Func <object[], object> GetConstructorDelegate(ConstructorInfo constructorInfo)
        {
            var targetParExp = Expression.Parameter(typeof(object), "s");
            var argsParExp   = Expression.Parameter(typeof(object[]), "args");

            var callExp   = InvokerBuilder.BuildMethodCall(constructorInfo, typeof(object), targetParExp, argsParExp);
            var lambdaExp = Expression.Lambda(typeof(Func <object[], object>), callExp, argsParExp);

            return((Func <object[], object>)lambdaExp.Compile());

            /*
             * var dm = new DynamicMethod("CreateInstance", typeof(object),
             *  new Type[] { typeof(object[]) }, constructorInfo.DeclaringType, true);
             *
             * var parameters = constructorInfo.GetParameters();
             * var emiter = new EmitHelper(dm.GetILGenerator());
             * emiter.nop
             *  .For(0, parameters.Length, (e, i) => e.ldarg_1.ldc_i4(i).ldelem_ref
             *      .Assert(parameters[i].ParameterType.IsValueType,
             *          e1 => e1.unbox_any(parameters[i].ParameterType),
             *          e1 => e1.castclass(parameters[i].ParameterType)))
             *  .newobj(constructorInfo)
             *  .ret();
             *
             * return (Func<object[], object>)dm.CreateDelegate(typeof(Func<object[], object>));
             */
        }
Example #2
0
        private static Func <object, object[], object> CreateInvokeDelegate(MethodInfo methodInfo)
        {
            var targetParExp = Expression.Parameter(typeof(object), "s");
            var argsParExp   = Expression.Parameter(typeof(object[]), "args");

            var callExp   = InvokerBuilder.BuildMethodCall(methodInfo, typeof(object), targetParExp, argsParExp);
            var lambdaExp = Expression.Lambda(typeof(Func <object, object[], object>), callExp, targetParExp, argsParExp);

            return((Func <object, object[], object>)lambdaExp.Compile());
        }
Example #3
0
        private Func <object[], object> InitializeInvoker(ConstructorInfo constructorInfo)
        {
            var targetParameterExpression = Expression.Parameter(typeof(object), "s");
            var argsParameterExpression   = Expression.Parameter(typeof(object[]), "args");

            var callExpression   = InvokerBuilder.BuildMethodCall(constructorInfo, typeof(object), targetParameterExpression, argsParameterExpression);
            var lambdaExpression = Expression.Lambda(typeof(Func <object[], object>), callExpression, argsParameterExpression);
            var compiled         = (Func <object[], object>)lambdaExpression.Compile();

            return(compiled);
        }