static object Evaluator(string code )
{
ICodeCompiler compiler;
compiler = new JScriptCodeProvider().CreateCompiler();
CompilerParameters parameters;
parameters = new CompilerParameters();
parameters.GenerateInMemory = true;
parameters.GenerateExecutable = true;
parameters.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);
CompilerResults results;
results = compiler.CompileAssemblyFromSource(parameters, code);
if (results.Errors.Count > 0)
throw new Exception(results.Errors[0 ].ErrorText);
Assembly assembly = results.CompiledAssembly;
MethodInfo entryPoint = assembly.EntryPoint;
return entryPoint.Invoke(null, new object[] { null } );
}
[Test]
public void EvaluatorTest()
{
Evaluator( "Context.Current.Data = \"Craig\"" );
Assert.AreEqual( "Craig", Context.Current.Data );
}
This seems elegant and able to handle a lot of things like side effects. Unfortunately I've been back on the server side for the most part and haven't really tried to put this code through its paces.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.