CSharpScript. DynamicClass as globals getting CS0234 error

165 Views Asked by At

I'm trying to build flexible script evaluator, which сould receive dynamic list of global variables. I'm using DynamicClass from DynamicLinq lib for build global object. But I've got CS0234 error:

The type or namespace name 'Dynamic' does not exist in the namespace 'System.Linq'

My problem is that my global type instance generated dynamically, and I can't add dynamic assembly to references. InteractiveAssemblyLoader doesn't work too.

I know about DynamicExpression-s, but I'm trying to find pure solution.

        var type = DynamicClassFactory.CreateType(
        new List<DynamicProperty>()
        {
            new DynamicProperty("X", typeof(int)),
            new DynamicProperty("Y", typeof(int))
        });

        var data = Activator.CreateInstance(type) as DynamicClass;
        data.SetDynamicPropertyValue("X", 1);
        data.SetDynamicPropertyValue("Y", 2);
        
        using (var loader = new InteractiveAssemblyLoader())
        {
            loader.RegisterDependency(type.Assembly);
            loader.RegisterDependency(typeof(ExpandoObject).Assembly);
            var script = CSharpScript.Create<int>("X + Y",
                ScriptOptions.Default
                    .AddReferences("System.Linq")
                    .AddReferences("System.Linq.Expressions")
                    .AddReferences("System.Linq.Dynamic.Core.DynamicClasses")
                    .AddImports("System.Linq")
                    .AddImports("System.Dynamic")
                    .AddImports("System.Linq.Dynamic.Core.DynamicClasses"),
                globalsType: type,
                assemblyLoader: loader 
                );
            var t = script.Compile();
            var p = await script.RunAsync(data);
        }
0

There are 0 best solutions below