51 o'clock to a friend, he asked a little question, just write dozens of lines of code can be a good illustration of the problem. But the machine is not installed VS, had to do. When you come back, think about it, if you have an online c#ide. So the internet to check the relevant information, the whole out a simple online c#ide.
To do this, mainly to solve two problems, first, if the text box on the page to compile and execute the code, the second is if the program to run the results of the output on the Web page.
The first question is not difficult. NET already has ready-made C # compile class CSharpCodeProvider (or other language), then use CompilerParameters class as compile parameter, can easily realize.
The second question, in the simplest case, is to display the content of the Console.Write method output on the Web page. This is also very good to do, as long as the output statement before compiling a replacement, the output of the content to save to another place. When the run is over, take it out of the place.
The code implementation is as follows:
Code
[Copy to Clipboard] CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Vsonline.framework
{
/**////<summary>
///Custom Output class
///</summary>
public class Consoler
{
//Storage of all output
public static dictionary<string, consoler> outputs {get; set;}
Static Consoler ()
{
outputs = new dictionary<string, consoler> ();
}
output Operation #region output Operation
//Current output
public list<string> Output {get; private set;}
public Consoler ()
{
Output = new list<string> ();
}
public void Write (object str)
{
Output.add (str. ToString ());
}
public void WriteLine (object str)
{
Output.add (str. ToString () + "\ n");
}
#endregion
}
}
using System;
using System.Reflection;
using Microsoft.csharp;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Vsonline.framework
{
/**////<summary>
///code Execution Class
///</summary>
public class Coderun
{
/**////<summary>
///Framework version, selectable v2.0, v3.0, v3.5
///</summary>
private string CompilerVersion {get; set;}
/**////<summary>
///Construction Function
///</summary>
///<param name= "CompilerVersion" >framework version, selectable v2.0, v3.0, v3.5</param>
public Coderun (string compilerversion)
{
compilerversion = compilerversion;
}
/**////<summary>
///Constructor, defaults to version 3.5
</summary>
public Coderun ()
{
compilerversion = "v3.5";
}
/**////<summary>
///dynamically compiles and executes code
///</summary>
///<param name= "code" > Codes </param>
///<returns> return output </returns>
Public list<string> Run (string code, string ID, params string[] assemblies)
{
Consoler.Outputs.Add (ID, new Consoler ());
CompilerParameters compilerparams = new CompilerParameters ();
//compiler option settings
compilerparams.compileroptions = "/target:library/optimize";
//compilerparams.compileroptions + = @ "/lib:" "C:\Program files\reference Assemblies\microsoft\framework\v3.5\" "";
//compile-time output in memory
Compilerparams.generateinmemory = true;
//Generate debugging Information
compilerparams.includedebuginformation = false;
//Add related references
foreach (string assembly in assemblies)
{
COMPILERPARAMS.REFERENCEDASSEMBLIES.ADD (assembly);
}
compilerParams.ReferencedAssemblies.Add ("mscorlib.dll");
compilerParams.ReferencedAssemblies.Add ("System.dll");
if (this.compilerversion = = "v3.5")
{
compilerParams.ReferencedAssemblies.Add ("System.Core.dll");
}
String path = "";
Try
{
Path = HttpContext.Current.Server.MapPath ("/bin/");
}
Catch {}
COMPILERPARAMS.REFERENCEDASSEMBLIES.ADD (path + "VSOnline.Framework.dll");
CSharpCodeProvider compiler = new CSharpCodeProvider (new dictionary<string, string> () {{"Compilerversi On ", CompilerVersion}});
//Compiling
Code = code. Replace ("Console.WriteLine", String. Format ("vsonline.framework.consoler.outputs[\" {0}\). WriteLine ", id));
Code = code. Replace ("Console.Write", String. Format ("vsonline.framework.consoler.outputs[\" {0}\). Write ", id));
compilerresults results = Compiler.compileassemblyfromsource (compilerparams, code);
//Error
if (results. Errors.haserrors)
{
foreach (CompilerError Error in results.) Errors)
{
Consoler.outputs[id]. Output.add (Error. ErrorText + "\ n");
}
return Returnoutput (ID);
}
//CREATE ASSEMBLY
Assembly asm = results.compiledassembly;
//Get the compiled type
Object mainclass = asm. CreateInstance ("program");
Type mainclasstype = Mainclass.gettype ();
//Output Results
Mainclasstype.getmethod ("Main"). Invoke (MainClass, NULL);
return Returnoutput (ID);
}
Private list<string> Returnoutput (string id)
{
string[] output = new String[consoler.outputs[id]. Output.count];
Consoler.outputs[id]. Output.copyto (output, 0);
Consoler.Outputs.Remove (ID);
return output. ToList ();
}
}
}