一个应用程序可以编译并创建另一个应用程序吗?
假设我们有一个名为 Program1.exe 的应用程序,因此当我单击该 exe 时,该程序必须创建另一个 exe,假设 Program2.exe 在屏幕上写下“hello world”。所以我认为可以使用 Visual Studio 的命令行工具在代码中使用 csc 命令,无论如何,如果可能的话,Program2.exe 可以替换 Program1.exe 吗?我的意思是 Program1.exe 可以在运行时重新编译吗?
Possible Duplicate:
How to programatically build and compile another c# project from the current project
Lets say we have an application named Program1.exe so when i click that exe that program has to create another exe lets say Program2.exe which write "hello world" on screen. So I think its possible using command line tool of visual studio using csc command in code, anyway if its possible can that Program2.exe replace Program1.exe? I mean that Program1.exe can re-compile in runtime?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保你可以做到这一点,看看这个类:
Microsoft .CSharp.CSharpCodeProvider
以及本文:编译和在运行时执行代码
PS,根据我的理解,您并不是真正从代码中调用 csc 命令,而是更低的命令......无论如何,当您使用托管类而不是外部工具时,这都是好的。
sure you can do this, have a look at the class:
Microsoft.CSharp.CSharpCodeProvider
and at this article: Compiling and Executing Code at Runtime
P.S. you are not really calling the csc command from code, but something way lower, in my understanding... which is good anyway as you use managed classes and not an external tool.
当 Program1.exe 运行时,Windows 应拒绝对该文件的写访问...
如果我正确理解你的问题,我会说这绝对不是正确的选择。为了在运行时改变自身,程序应该使用某种形式的反射。
如果您想高效地完成工作,请查看 Emit,这为您提供了完整的字节码访问能力(以及相关的危险)。
几年前,我使用了 CodeProject 的一个项目,它帮助我解决了细节问题。我正在研究 .NET 2.0,现在肯定有一些更新的东西(比如 this)。
或者您可以使用 CSharpCodeProvider,并将程序集构建为已加载(从源代码或树表示编译)。
您也可以使用 csc 编译程序集,并将其加载到正在运行的程序中。如果您采用这种方式,则需要在程序集加载时注意一些。 此链接可能很有用。
While Program1.exe is running, Windows should deny write access to the file...
If I understood correctly your question, I'd say that definitely it's not the way to go. To change itself during runtime, a program should use some form of Reflection.
If you want to do efficiently, look at Emit, that gives you full bytecode access power (and related danger).
Some years ago I used a project from CodeProject, that helped me to work out the details. I was working on .NET 2.0, now surely there is something more recent (like this).
Or you could use CSharpCodeProvider, and build assemblies to be loaded (either compile from source or from a tree representation).
You could as well use csc to compile an assembly, and load that in your running program. If you go this way, you'll need some attention in Assembly loading. This link could be useful.