Reputation: 1171
I had this idea: create a program that simulates the visual studio to help people learn c#. It would be something like that "try for yourself" feature of w3schools.com
There would be a textbox that the user could put some code in it for example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
}
}
}
but now there's the tricky part: if the user commited a synthax error for example, i'd like to show the error like the visual studio, or when the user clicks on a "verify" button, if the code is wrong, i'd get the same error as the visual studio would show up. Is it possible?
Upvotes: 0
Views: 75
Reputation: 1171
Thanks for the answers. I've found what i've been looking for. What i wanted to do, is to allow an user to input some code in a text box, and when he/she clicks on a "generate" button, the code would be compiled and an .exe would be generated. After some research, ive found this class:
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
You can set some parameters to generate an .exe or a dll and you send a string as the code to be compiled. If it has errors, you can catch them by doing this:
if (results.Errors.Count > 0)
{
{
foreach (CompilerError CompErr in results.Errors)
{
txtErro.Text = txtErro.Text +
"Line number " + CompErr.Line +
", Error Number: " + CompErr.ErrorNumber +
", '" + CompErr.ErrorText + ";" +
Environment.NewLine + Environment.NewLine;
}
}
}
I hope that this can be useful to the others as well. Thanks a lot!
Upvotes: 1
Reputation: 15367
I think it is possible but you have to use reflection. With that you can dynamically create C# code which you can compile runtime. It is however quite advanced but as far as I know it is possible.
The following link might be useful:
Generating DLL assembly dynamically at run time
What you really want is:
What are the benefits of Compiler as a Service
But afaik this is not available now. Also you do not get the debugging functionality. You can probably run a new instance of VS with debugging etc, but it might be a little overkill.
Upvotes: 0