从 ASP.NET 网站编译代码

发布于 2024-10-17 07:24:51 字数 129 浏览 1 评论 0原文

我需要在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜清冷一曲。 2024-10-24 07:24:51

您需要托管您想要支持和使用其 API 的各种编译器。如果编译器未公开 API,您需要使用 System.Diagnostics.Process 来启动它并获取输出进行解析。

但这绝不是一项微不足道的任务。
如果您确信需要这个,请问自己几个问题。

  • 您计划支持哪些语言、平台和语言? Python 和 Ruby,您是指它们的 .NET 版本还是想使用它们的标准编译器?哪些版本?
  • 这些编译器有公共 API 吗?您需要将它们作为可执行文件运行吗?
  • 如果该站点将由许多用户同时使用,您计划如何分配负载?编译是一个密集的过程,很多人这样做会使您的服务器超载。另请注意,编译总是需要时间,因此您需要考虑如何将其呈现给用户。您希望它立即启动还是希望将用户放入队列中?
  • 用户编码范围是什么?用户输入是否仅限于一种方法内容,还是用户必须写入整个文件?系统默认添加了什么代码吗?如何解析 C# 代码等引用?是否默认引用了某些库?

这只是冰山一角。

回答你的评论,是的,有一个用于 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.

  • Which languages, platforms and languages do you plan to support? Python and Ruby, do you mean .NET versions of them or do you want to use their standard compilers? Which versions?
  • Do these compilers have any public APIs? Will you need to run them as executables?
  • If the site is going to be used concurrently by many users, how do you plan to distribute the load? Compiling is an intensive process and having a lot of people doing it will overload your server. Also note that compilation always takes time so you'll need to think how to present it to user. Do you want it to start immediately or do you want to put users in a queue?
  • What is the user coding scope? Is user input limited to one method contents, or does the user have to write the whole file? Is any code added by system by default? How do you resolve the references e.g. for C# code? Are some libraries referenced by default?

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.

伴我老 2024-10-24 07:24:51

您可能需要查看 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).

迟到的我 2024-10-24 07:24:51

对于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文