在运行时使用 .NET 3.5 和 C# 从文件编译 IL 代码 [英] Compile IL code at runtime using .NET 3.5 and C# from file

查看:23
本文介绍了在运行时使用 .NET 3.5 和 C# 从文件编译 IL 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取一个 IL 文件,并在运行时将其编译回 exe.

I would like to take a file that is an IL file, and at run time compile it back to an exe.

现在我可以使用 process.start 通过参数 (ilasm.exe) 启动命令行,但我想从我将创建的 C# 服务中自动执行此过程.

Right now I can use process.start to fire off the command line with parameters (ilasm.exe) but I would like to automate this process from a C# service I will create.

有没有办法用反射和反射.emit来做到这一点?

Is there a way to do this with reflection and reflection.emit?

虽然这有效:

string rawText = File.ReadAllText(string.Format("c:\temp\{0}.il", Utility.GetAppSetting("baseName")), Encoding.ASCII);

rawText = rawText.Replace("[--STRIP--]", guid);

File.Delete(string.Format("c:\temp\{0}.il", Utility.GetAppSetting("baseName")));

File.WriteAllText(string.Format("c:\temp\{0}.il", Utility.GetAppSetting("baseName")),rawText, Encoding.ASCII);

pi = new ProcessStartInfo();
pi.WindowStyle = ProcessWindowStyle.Hidden;
pi.FileName = """ + ilasm + """;
pi.Arguments = string.Format("c:\temp\{0}.il", Utility.GetAppSetting("baseName"));

using(Process p = Process.Start(pi))
{
    p.WaitForExit();
}

这并不理想,因为我真的希望这是一个简化的流程.

It is not ideal as I really would like this to be a streamlined process.

我看过在运行时创建 IL 然后保存的示例,但我需要使用我已经拥有的文件形式的 IL 并将其编译回 exe.

I have seen examples of creating the IL at runtime, then saving, but I need to use the IL I already have in file form and compile it back to an exe.

谢谢.

推荐答案

简化流程"是什么意思?对你来说它的表现还不够好吗?我知道产生一个单独的进程感觉有点脏,但这是我能想到的最简单的方法.使用适当的权限,您应该仍然可以从服务中执行此操作.

What do you mean by a "streamlined process"? Is it not performing well enough for you? I know it feels slightly dirty to spawn a separate process, but it's the simplest way I can think of. With the appropriate permissions you should still be able to do this from a service.

你想要像 CSharpCodeProvider 这样的东西,但是对于 IL;恐怕我不知道有这样的课程.

You want something like CSharpCodeProvider but for IL; I don't know of any such class, I'm afraid.

这篇关于在运行时使用 .NET 3.5 和 C# 从文件编译 IL 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆