从 ASP.NET 网站编译代码
我需要在 ASP.NET 页面上有一个文本框,用户可以在其中粘贴代码并选择一种预设语言(C#、VB.NET、Python、Ruby 等),并且我需要验证代码是否成功编译。如果没有,那么我需要用行号显示错误和警告。
我该怎么做呢?
I need to have a textbox on a ASP.NET page in which a user would paste code and select one of the preset languages (C#, VB.NET, Python, Ruby etc) and I need to verify if the code compiles successfully. If it doesn't, then I need to show the errors and warnings with line numbers.
How do I go about doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要托管您想要支持和使用其 API 的各种编译器。如果编译器未公开 API,您需要使用 System.Diagnostics.Process 来启动它并获取输出进行解析。
但这绝不是一项微不足道的任务。
如果您确信需要这个,请问自己几个问题。
这只是冰山一角。
回答你的评论,是的,有一个用于 C# 和 VB .NET 的编译器 API,名为 CodeDOM。
网上有很多关于它的信息。您可能想查看这个问题 也是如此。
You'll need to host all sorts of compilers you'd like to support and use their APIs. If a compiler doesn't expose an API, you'd need to use
System.Diagnostics.Process
to launch it and grab the output for parsing.This is far from a trivial task though.
If you're confident you need this, ask yourself several questions.
And this is just the tip of the iceberg.
Answering your comment, yes, there is a compiler API for C# and VB .NET which is called CodeDOM.
There's plenty of information about it on the net. You may want to check out this question as well.
您可能需要查看 Roslyn 项目(以前的编译器即服务),网址为 http:// msdn.microsoft.com/en-us/roslyn 于昨天发布。这是针对 C# 和 VB 的新编译器 API。它对 Python 或 Ruby 没有帮助,但一旦发布(Dev 11 后),应该可以满足 C# 和 VB 的需要。
You may want to take a look at the Roslyn project (formerly Compiler as a Service) at http://msdn.microsoft.com/en-us/roslyn which was released yesterday. This is the new compiler APIs coming for C# and VB. It won't help for Python or Ruby but should do what you need for C# and VB once it ships (post Dev 11).
对于 C# 和 VB,使用 Roslyn CTP 创建 Compilation 对象,然后调用 GetDiagnostics() 方法来确定错误。
For C# and VB, using the Roslyn CTP to create a Compilation object, then call the GetDiagnostics() method to determine the errors.