Note that there are some explanatory texts on larger screens.

plurals
  1. POMono.CSharp: how do I inject a value/entity *into* a script?
    primarykey
    data
    text
    <p>Just came across the latest build of Mono.CSharp and love the promise it offers.</p> <p>Was able to get the following all worked out:</p> <pre><code>namespace XAct.Spikes.Duo { class Program { static void Main(string[] args) { CompilerSettings compilerSettings = new CompilerSettings(); compilerSettings.LoadDefaultReferences = true; Report report = new Report(new Mono.CSharp.ConsoleReportPrinter()); Mono.CSharp.Evaluator e; e= new Evaluator(compilerSettings, report); //IMPORTANT:This has to be put before you include references to any assemblies //our you;ll get a stream of errors: e.Run("using System;"); //IMPORTANT:You have to reference the assemblies your code references... //...including this one: e.Run("using XAct.Spikes.Duo;"); //Go crazy -- although that takes time: //foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) //{ // e.ReferenceAssembly(assembly); //} //More appropriate in most cases: e.ReferenceAssembly((typeof(A).Assembly)); //Exception due to no semicolon //e.Run("var a = 1+3"); //Doesn't set anything: //e.Run("a = 1+3;"); //Works: //e.ReferenceAssembly(typeof(A).Assembly); e.Run("var a = 1+3;"); e.Run("A x = new A{Name=\"Joe\"};"); var a = e.Evaluate("a;"); var x = e.Evaluate("x;"); //Not extremely useful: string check = e.GetVars(); //Note that you have to type it: Console.WriteLine(((A) x).Name); e = new Evaluator(compilerSettings, report); var b = e.Evaluate("a;"); } } public class A { public string Name { get; set; } } } </code></pre> <p>And that was fun...can create a variable in the script's scope, and export the value. </p> <p>There's just one last thing to figure out... how can I get a value in (eg, a domain entity that I want to apply a Rule script on), without using a static (am thinking of using this in a web app)?</p> <p>I've seen the use compiled delegates -- but that was for the previous version of Mono.CSharp, and it doesn't seem to work any longer. </p> <p>Anybody have a suggestion on how to do this with the current version?</p> <p>Thanks very much.</p> <p>References: * <a href="https://stackoverflow.com/questions/3788096/injecting-a-variable-into-the-mono-csharp-evaluator-runtime-compiling-a-linq-qu">Injecting a variable into the Mono.CSharp.Evaluator (runtime compiling a LINQ query from string)</a> * <a href="http://naveensrinivasan.com/tag/mono/" rel="nofollow noreferrer">http://naveensrinivasan.com/tag/mono/</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload