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 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 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)); } }
public void CreateExceptionObjectOfNestedClassWithinScriptSeparateAppDomain_Failure() { using (var script = new CSharpScript()) { script.ExecuteInSeparateAppDomain = true; script.AddMember( @"[Serializable] public class ETestClass : Exception {} "); script.CreateScriptObject("ETestClass"); } }
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 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)); } }