I'm working with scripting rules engine based on roslyn-ctp that will process IEnumerable<T>
and returns results as IEnumerable<T>
. To avoid creating of ScriptEngine
, configuring and parsing again and again, I'd like to reuse one instance of ScriptEngine
.
Here is a short sample (full sample at gist.github.com):
var engine = new ScriptEngine();
new[]
{
typeof (Math).Assembly,
this.GetType().Assembly
}.ToList().ForEach(assembly => engine.AddReference(assembly));
new[]
{
"System", "System.Math",
typeof(Model.ProcessingModel).Namespace
} .ToList().ForEach(@namespace => engine.ImportNamespace(@namespace));
IEnumerable<Model.ProcessingModel> models = new[]
{
new Model.ProcessingModel { InputA = 10M, InputB = 5M, Factor = 0.050M },
new Model.ProcessingModel { InputA = 20M, InputB = 2M, Factor = 0.020M },
new Model.ProcessingModel { InputA = 12M, InputB = 3M, Factor = 0.075M }
};
// no dynamic allowed
// anonymous class are duplicated in assembly
var script =
@"
Result = InputA + InputB * Factor;
Delta = Math.Abs((Result ?? 0M) - InputA);
Description = ""Some description"";
var result = new { Σ = Result, Δ = Delta, λ = Description };
result
";
// Here is ArgumentException `Duplicate type name within an assembly`
IEnumerable<dynamic> results =
models.Select(model => engine.CreateSession(model).Execute(script));
And here are several issues:
- roslyn-ctp does not support
dynamic
keyword - While using anonymous types inside script I get an exception
Duplicate type name within an assembly
when rsolyn-ctp is creating assembly usingSystem.Reflection.Emit
Question
Is there way to create ScriptEngine
and reuse it many times when script contains anonymous type?