public void ExpressionWithTwoParameters_Success()
 {
     using (var expression = new CSharpScript("1 + a + c"))
     {
         expression.AddObjectInScope("a", 1);
         expression.AddObjectInScope("c", 3);
         Assert.AreEqual(5, expression.Execute());
     }
 }
 public void ExpressionWithSameParameterTwice_Failure()
 {
     using (var expression = new CSharpScript("1 + a + a"))
     {
         expression.AddObjectInScope("a", 1);
         expression.AddObjectInScope("a", 3);
         Assert.AreEqual(5, expression.Execute());
     }
 }
 public void ExpressionWithOneParameterForceRecompilation_Success()
 {
     using (var expression = new CSharpScript("1 + a"))
     {
         expression.AddObjectInScope("a", 1);
         Assert.AreEqual(2, expression.Execute());
         expression.AddObjectInScope("b", 2);
         Assert.AreEqual(2, expression.Execute());
     }
 }
 public void ExpressionReferencingTwoLocalObjects_Success()
 {
     using (var expression = new CSharpScript("obj2.int_Field + obj1.int_Field"))
     {
         var obj1 = new TestClass {
             int_Field = 2
         };
         var obj2 = new TestClass {
             int_Field = 3
         };
         expression.AddObjectInScope("obj1", obj1);
         expression.AddObjectInScope("obj2", obj2);
         Assert.AreEqual(5, expression.Execute());
     }
 }
示例#5
0
        static void Main(string[] args)
        {
            var expression = new CSharpScript("1 + a.a")
            {
                ExecuteInSeparateAppDomain = true
            };

            // This snippet shows how a class on a assembly in the GAC can be used
            using (expression)
            {
                expression.AddObjectInScope("a", new TestClassInGACAssembly());
                Console.WriteLine(expression.Execute());
            }

            // This snippet shows how building a SovosExpando, in an assembly installed in GAC works fine.
            // You can call all methods dynamically from the C# script
            using (expression = new CSharpScript()
            {
                ExecuteInSeparateAppDomain = true
            })
            {
                var expando = SovosExpandoBuilder.Build();
                expression.AddObjectInScope("sovosExpando", expando);
                expression.AddCodeSnippet(
                    @"var v = sovosExpando.Test; // We read here a property that gets created on-the-fly
            sovosExpando.Test = ""Hola Mundo""; // We enter a value here that will be cleared by ResetTest() call
            sovosExpando.ResetTest(); // We invoke a dynamically created method here
            sovosExpando.Test = v + sovosExpando.Test + ""Hello World""; // We use here the property read on-the-fly and stored in v
            return sovosExpando.Test");
                Console.WriteLine(expression.Execute());
            }
        }
 public void BasicExpressionSeparateAppDomainShareIntegerObject_Success()
 {
     using (var expression = new CSharpScript("1 + a"))
     {
         expression.ExecuteInSeparateAppDomain = true;
         expression.AddObjectInScope("a", 1);
         Assert.AreEqual(2, expression.Execute());
     }
 }
 public void UseExpandoObjectWithDynamicMethod_Success()
 {
     using (var expression = new CSharpScript("\"a\" + a.Test(\"a\")"))
     {
         dynamic expando = new ExpandoObject();
         expando.Test = new Func <string, string>(str => "Hello" + str);
         expression.AddObjectInScope("a", expando);
         Assert.AreEqual("aHelloa", expression.Execute());
     }
 }
 public void ExpressionReferencingExpandoObject_Success()
 {
     using (var expression = new CSharpScript("1 + obj.int_Field"))
     {
         IDictionary <string, object> obj = new ExpandoObject();
         obj.Add("int_Field", 2);
         expression.AddObjectInScope("obj", obj);
         Assert.AreEqual(3, expression.Execute());
     }
 }
 public void ExpressionReferencingLocalObject_Success()
 {
     using (var expression = new CSharpScript("1 + obj.int_Field"))
     {
         var obj = new TestClass {
             int_Field = 2
         };
         expression.AddObjectInScope("obj", obj);
         Assert.AreEqual(3, expression.Execute());
     }
 }
 public void ShortPerformanceTest_Success()
 {
     using (var expression = new CSharpScript("1 + obj.int_Field"))
     {
         var obj = new TestClass();
         expression.AddObjectInScope("obj", obj);
         var initialTicks = Environment.TickCount;
         for (obj.int_Field = 1; obj.int_Field < 1000000; obj.int_Field++)
         {
             Assert.AreEqual(1 + obj.int_Field, expression.Execute());
         }
         Assert.Less(Environment.TickCount - initialTicks, 2000); // 1MM iterations should run in less than 1 second
     }
 }
 public void UseCustomExpandoObjectWithProperty_Success()
 {
     using (var expression = new CSharpScript())
     {
         var expando = SovosExpandoBuilder.Build();
         expression.AddObjectInScope("sovosExpando", expando);
         expression.AddCodeSnippet(
             @"var v = sovosExpando.Test; // We read here a property that gets created on-the-fly
     sovosExpando.Test = ""Hola Mundo""; // We enter a value here that will be cleared by ResetTest() call
     sovosExpando.ResetTest(); // We invoke a dynamically created method here
     sovosExpando.Test = v + sovosExpando.Test + ""Hello World""; // We use here the property read on-the-fly and stored in v
     return sovosExpando.Test");
         Assert.AreEqual("Hello World", expression.Execute());
     }
 }
 public void RunExpressionTwiceReferencingReplacedLocalObject_Success()
 {
     using (var expression = new CSharpScript("1 + obj.int_Field"))
     {
         var obj = new TestClass {
             int_Field = 2
         };
         expression.AddObjectInScope("obj", obj);
         Assert.AreEqual(3, expression.Execute());
         var obj2 = new TestClass {
             int_Field = 3
         };
         expression.ReplaceObjectInScope("obj", obj2);
         Assert.AreEqual(4, expression.Execute());
     }
 }
 public void CreateTwoObjectsWithObjectInScopeMaintainingDifferentState_Success()
 {
     using (var script = new CSharpScript())
     {
         script.AddMember(
             @"public void Test(int a) {  
       global.a = a;                            
     }");
         script.AddExpression("global.a + i");
         script.AddObjectInScope("i", 1);
         var obj_a = script.CreateScriptObject();
         var obj_b = script.CreateScriptObject();
         Assert.AreEqual(null, script.Invoke(obj_a, "Test", new object[] { 5 }));
         Assert.AreEqual(null, script.Invoke(obj_b, "Test", new object[] { 10 }));
         Assert.AreEqual(6, script.Execute(obj_a));
         Assert.AreEqual(11, script.Execute(obj_b));
     }
 }