首页 文章

静态链接和编译Runtime w / compiletoMethod()表达式树

提问于
浏览
6

我正在开发一个项目,我们正在使用DLR将Racket语言移植到.NET .

我们构建一个表达式树并调用 CompileToMethod() 方法:

相关的可执行发射代码:(取自How to Save an Expression Tree as the Main Entry Point to a New Executable Disk File?

//Wrap the program into a block expression
Expression code = Expression.Block(new ParameterExpression[] { env, voidSingleton}, program);

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.Lambda<Action>(code).CompileToMethod(methodBuilder);

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

我们有我们的运行时库 Runtime_rkt.dll ,它包含相关的运行时类型转换,后备对象等 .

当我们将 Foo.exeRuntime_rkt.dll 放在同一目录中时,一切正常 . 我们遇到的问题是当我们(显然)将运行时库移动到其他地方时 . 最终我们希望像IronPython一样在_1233921中安装它 . [使用GAC解决]

[edit] New Question for Extra pts Is there any way we can statically compile all the runtime methods into the executable?

1 回答

  • 0
    • 将运行时库放到GAC后,一切都应该有效,你现在不应该这样做吗?

    • 尝试自己解释构建的可执行文件如何同时定位运行时库

    • 了解ILMerge

    • 查看.net程序集的新本机编译MS今年正在推出 . 它被广告不仅编译为本机代码,还包括所有库和CLR本身到exe文件中 .

    • 您始终可以订阅AppDomain.AssemblyResolve事件 . 显然,您必须将该订阅编译到生成的exe文件中 .

    • 您可以配置CLR在配置文件中查找DLL的文件夹 .

相关问题