How to Save an Expression Tree as the Main Entry Point to a New Executable Disk File?

六月ゝ 毕业季﹏ 提交于 2019-12-04 13:12:25

问题


I'm trying to export an expression tree to a PE assembly as the main entry point. I've acquired a Lambda Expression through building an expression tree, for example:

using System.Linq;
using System;

// 1. use expression trees to create a block expression (not shown)

// 2. create a lambda expression: 
LambdaExpression exprLambda = Expression.Lambda(exprBlock, new ParameterExpression[0]);

MethodBuilder mbuilder = null;
// 3. ??? Somehow get a method builder instance that works ??? 

// 4. Compile the lambda using the MethodBuilder instance. 
exprLambda.CompileToMethod(mbuilder);

// 5. ??? Somehow get an AssemblyBuilder instance to .Save(..) this to disk.  ??? 

Steps 3 and 5 are what I'm missing.


回答1:


Instead of just using Expression.Compile, use Expression.CompileToMethod(MethodBuilder).

Short but complete example which creates an executable on disk, with the expression tree as the code executed in the entry point:

using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Linq.Expressions;

class Program
{
    static void Main()
    {
        var asmName = new AssemblyName("Foo");
        var asmBuilder = AssemblyBuilder.DefineDynamicAssembly
            (asmName, AssemblyBuilderAccess.RunAndSave);
        var moduleBuilder = asmBuilder.DefineDynamicModule("Foo", "Foo.exe");

        var typeBuilder = moduleBuilder.DefineType("Program", TypeAttributes.Public);
        var methodBuilder = typeBuilder.DefineMethod("Main",
            MethodAttributes.Static, typeof(void), new[] { typeof(string) });

        Expression<Action> action = () => Console.WriteLine("Executed!");

        action.CompileToMethod(methodBuilder);

        typeBuilder.CreateType();
        asmBuilder.SetEntryPoint(methodBuilder);
        asmBuilder.Save("Foo.exe");
    }
}


来源:https://stackoverflow.com/questions/15168150/how-to-save-an-expression-tree-as-the-main-entry-point-to-a-new-executable-disk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!