简体   繁体   English

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

[英]Compile IL code at runtime using .NET 3.5 and C# from file

I would like to take a file that is an IL file, and at run time compile it back to an exe. 我想获取一个IL文件,并在运行时将其编译回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. 现在,我可以使用process.start来启动带有参数(ilasm.exe)的命令行,但是我想从我将创建的C#服务中自动执行此过程。

Is there a way to do this with reflection and reflection.emit? 有没有办法用反射和reflect.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. 我已经看到了在运行时创建IL,然后保存的示例,但是我需要使用文件格式中已有的IL并将其编译回exe。

Thanks. 谢谢。

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; 您需要类似CSharpCodeProvider的东西,但要使用IL。 I don't know of any such class, I'm afraid. 恐怕我不知道上这样的课。

Actually, many obfuscators, like preemtive and BitHelmet uses ilasm.exe and Process. 实际上,许多混淆器(例如preemtiveBitHelmet)都使用ilasm.exe和Process。 I think it is the best strategy. 我认为这是最好的策略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM