MonoCSharp Evaluator Extension

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.