Joh*_*n K 5 delegates dynamic-assemblies
我正在尝试将表达式树导出到PE程序集作为主要入口点.我通过构建表达式树获得了Lambda Expression,例如:
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. ???
Run Code Online (Sandbox Code Playgroud)
第3步和第5步是我所缺少的.
Jon*_*eet 11
而不仅仅是使用Expression.Compile
,使用Expression.CompileToMethod(MethodBuilder)
.
简短而完整的示例,它在磁盘上创建可执行文件,表达式树作为在入口点中执行的代码:
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");
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1050 次 |
最近记录: |