使用 .NET 3.5 和 C# 从文件在运行时编译 IL 代码
我想获取一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“简化流程”是什么意思?它对你来说表现不够好吗?我知道生成一个单独的进程感觉有点脏,但这是我能想到的最简单的方法。有了适当的权限,您仍然应该能够从服务中执行此操作。
您想要类似 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.
实际上,有很多混淆器,例如 preemtive 和 BitHelmet 使用 ilasm.exe 和 Process。我认为这是最好的策略。
Actually, many obfuscators, like preemtive and BitHelmet uses ilasm.exe and Process. I think it is the best strategy.