Exemplo n.º 1
0
        static MethodInfo GenerateMethod(Expression expression, TO2Type returnType)
        {
            Context       context       = new Context(KontrolRegistry.CreateCore());
            ModuleContext moduleContext = context.CreateModuleContext("DynamicExpression");
            IBlockContext methodContext = moduleContext.CreateMethodContext(FunctionModifier.Public, false, "Exec",
                                                                            returnType, Enumerable.Empty <FunctionParameter>());

            expression.EmitCode(methodContext, false);
            Assert.False(methodContext.HasErrors);
            methodContext.IL.EmitReturn(methodContext.MethodBuilder.ReturnType);

            Type dynamicType = moduleContext.CreateType();

            return(dynamicType.GetMethod("Exec"));
        }