使用 .NET 3.5 和 C# 从文件在运行时编译 IL 代码

发布于 2024-08-30 19:28:39 字数 927 浏览 2 评论 0原文

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

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

有没有办法通过反射和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();
}

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

我见过在运行时创建 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.

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.

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

While this works:

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.

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.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

·深蓝 2024-09-06 19:28:39

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

您想要类似 CSharpCodeProvider 的东西,但用于 IL;恐怕我不知道有这样的课程。

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.

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

青衫负雪 2024-09-06 19:28:39

实际上,有很多混淆器,例如 preemtiveBitHelmet 使用 ilasm.exe 和 Process。我认为这是最好的策略。

Actually, many obfuscators, like preemtive and BitHelmet uses ilasm.exe and Process. I think it is the best strategy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文