Mono. CSharp. Evaluator provides convenient dynamic compilation functions, but the default Compile method,
Only methods without parameters are supported, such:
Mono.CSharp.Evaluator evaluator = new Mono.CSharp.Evaluator(new Mono.CSharp.CompilerSettings(), new Mono.CSharp.Report(new ConsoleReportPrinter()));
var method = evaluator.Compile("System.Console.WriteLine(\"dynamic compiled\");");
// or with return value.
method = evaluator.Compile("new System.DateTime();");
object value = null;
method(ref value);
In fact, it is easy to support methods with parameters, and Lambda expressions can be easily implemented:
evaluator.ReferenceAssembly(typeof(Form1).Assembly);
var action = (System.Action<WindowsFormsApplication1.Form1, object>)evaluator.Evaluate("new System.Action<WindowsFormsApplication1.Form1, object>((instance, value) => { instance.Test(value); });");
action(this, "test8");
So I wrote an EvaluatorExtension to simplify the Code:
var action = evaluator.Compile<Action<Form1, object>>("(instance, value) => { instance.Test(value); }");
action(this, "test12");
// or with return value
var func = evaluator.Compile<Func<Form1, object, string>>("(instance, value) => { instance.Test(value); return (string)value; }");
string value = func(this, "test13");
Alternatively, use the parameter method to define "parameter name ":
var action = evaluator.Compile<Action<Form1, object>>(new[] { "instance", "value" }, "instance.Test(value);");
Download Mono. CSharp. dll: Mono. CSharp. dll