English 中文(简体)
Compiling code from a ASP.NET website
原标题:

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?

最佳回答

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.

问题回答

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).

For C# and VB, using the Roslyn CTP to create a Compilation object, then call the GetDiagnostics() method to determine the errors.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签