public void CreateExceptionObjectOfNestedClassWithinScriptAndThrow_Success() { using (var script = new CSharpScript()) { script.AddMember( @"[Serializable] public class ETestClass : CSharpScriptException {} "); var obj = (Exception)script.CreateScriptObject("ETestClass"); /* access SetMessage() using ICSharpScriptObjectMethodInvoker */ var setter = (ICSharpScriptObjectMethodInvoker)obj; setter.Invoke("SetMessage", new object[] { "Hello World" }); /* access SetMessage() using dynamic object */ dynamic dynObj = obj; dynObj.SetMessage("Hello World"); try { throw obj; } catch (Exception e) { Assert.AreEqual("ETestClass", e.GetType().Name); Assert.AreEqual("Hello World", e.Message); } } }
public void InvokeMethodWithParams_Success() { using (var expression = new CSharpScript()) { expression.AddMember( @"public int Test(int a) { return a; }"); Assert.AreEqual(5, expression.Invoke("Test", new object[] { 5 })); } }
public void InvokeMethod_Success() { using (var expression = new CSharpScript()) { expression.AddMember( @"public int Test() { return 10; }"); Assert.AreEqual(10, expression.Invoke("Test", null)); } }
public void CreateExceptionObjectOfNestedClassWithinScriptSeparateAppDomain_Failure() { using (var script = new CSharpScript()) { script.ExecuteInSeparateAppDomain = true; script.AddMember( @"[Serializable] public class ETestClass : Exception {} "); script.CreateScriptObject("ETestClass"); } }
public void InvokeVoidMethodWithParamsModifyGlobals_Success() { using (var expression = new CSharpScript()) { expression.AddMember( @"public void Test(int a) { global.a = a; }"); expression.AddExpression("global.a"); Assert.AreEqual(null, expression.Invoke("Test", new object[] { 5 })); Assert.AreEqual(5, expression.Execute()); } }
public void CallInjectedFunction_Success() { using (var expression = new CSharpScript()) { expression.AddMember( @"private int AddNumbers(int a, int b) { return a + b; }"); Assert.AreEqual(0, expression.AddExpression("AddNumbers(1, 2)")); Assert.AreEqual(3, expression.Execute()); } }
public void ClassSnippet_Success() { using (var expression = new CSharpScript()) { expression.AddMember( @"private class Tester { public static int Test() { return 10; } }"); Assert.AreEqual(0, expression.AddCodeSnippet("var i = 1; return Tester.Test() + i")); Assert.AreEqual(11, expression.Execute()); } }
public void InvokeMethodWithParamsModifyGlobalsMultiAppDomain_Success() { using (var expression = new CSharpScript()) { expression.ExecuteInSeparateAppDomain = true; expression.AddMember( @"public int Test(int a) { global.a = 20; return a; }"); expression.AddExpression("global.a"); Assert.AreEqual(5, expression.Invoke("Test", new object[] { 5 })); Assert.AreEqual(20, expression.Execute()); } }
public void CreateObjectOfNestedClassWithinScriptObject_Success() { using (var script = new CSharpScript()) { script.AddMember( @"public class TestClass { public int a = 10; public TestClass() { } } "); dynamic obj = script.CreateScriptObject("TestClass"); Assert.AreEqual(10, obj.a); } }
public void CreateTwoObjectsMaintainingDifferentState_Success() { using (var script = new CSharpScript()) { script.AddMember( @"public void Test(int a) { global.a = a; }"); script.AddExpression("global.a"); 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(5, script.Execute(obj_a)); Assert.AreEqual(10, script.Execute(obj_b)); } }
public void CreateObjectOfNestedClassWithinScriptObjectUsingSeparateAppDomain_Success() { using (var script = new CSharpScript()) { script.ExecuteInSeparateAppDomain = true; script.AddMember( @"public class TestClass : CSharpScriptObjectBase { public int _a = 10; public TestClass() { } public int a() { return _a; } } "); var obj = (ICSharpScriptObjectMethodInvoker)script.CreateScriptObject("TestClass"); Assert.AreEqual(10, obj.Invoke("a", null)); } }
public void CreateTwoObjectsWithObjectInScopeResettingIt_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)); script.ReplaceObjectInScope(obj_a, "i", 2); Assert.AreEqual(7, script.Execute(obj_a)); Assert.AreEqual(11, script.Execute(obj_b)); } }