问题描述
我想借一个文件,是一个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.
有没有办法用反射和Reflection.Emit的
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.
我已经看到在运行时创建的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代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!