|
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
namespace Endogine.Scripting
{
/// <summary>
/// Summary description for ScripterCSharp.
/// </summary>
public class ScripterCSharp : ScripterBase
{
public ScripterCSharp()
{
this._defaultContext = @"
class MyProgram
{
static void Main()
{
###
}
}
";
}
public override string GenerateReferenceString()
{
string refs = "";
foreach (string ass in this._referencedAssemblies)
refs+="using "+ass+";";
return refs;
}
public override object Execute(string code)
{
ICodeCompiler compiler = new Microsoft.CSharp.CSharpCodeProvider().CreateCompiler();
CompilerParameters cmpParams = new System.CodeDom.Compiler.CompilerParameters();
cmpParams.GenerateInMemory = true;
cmpParams.GenerateExecutable = true;
//cmpParams.CompilerOptions = "/t:exe";
foreach (string ass in this._referencedAssemblies)
cmpParams.ReferencedAssemblies.Add(ass+".dll");
string codeString = this.GenerateReferenceString();
codeString = this.InsertCode(code);
CompilerResults results = compiler.CompileAssemblyFromSource(cmpParams, codeString);
// foreach (System.CodeDom.Compiler.CompilerError ce in results.Errors)
// this.Put(ce.ErrorText);
if (results.Errors.Count == 0 && results.CompiledAssembly!=null)
{
System.Reflection.MethodInfo methodInfo = results.CompiledAssembly.EntryPoint;
return methodInfo.Invoke(null, null);
}
return null;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.