public void LoadMethod_T() { ICalc calc = evaluator.LoadMethod <ICalc>("int Sum(int a, int b) => a+b;"); var result = calc.Sum(7, 3); Assert.Equal(10, result); }
public static void LoadAndUnloadImpl(string asmFile, out WeakReference alcWeakRef) { CSScript.Evaluator .ReferenceAssemblyOf <ICalc>() .CompileAssemblyFromCode( @"using System; public class Script : ICalc { public int Sum(int a, int b) { return a+b; } }", asmFile); var asm = new UnloadableAssembly(); alcWeakRef = new WeakReference(asm); ICalc script = (ICalc)asm.LoadFromAssemblyPath(asmFile) .CreateObject("*"); // or `CreateInstance("css_root+Script")` int result = script.Sum(1, 3); asm.Unload(); }
public static void LoadCode_WithDuckTypedInterface(HostApp host) { // 1 - LoadCode compiles code and returns instance of a first class in the compiled assembly // 2- The script class doesn't implement host app interface but it can still be aligned to // one as long at it implements the interface members // 3 - In this sample host object is passed into script routine. //This use-case uses Interface Alignment and this requires all assemblies involved to have //non-empty Assembly.Location CSScript.GlobalSettings.InMemoryAssembly = false; ICalc calc = CSScript.LoadCode(@"using CSScriptNativeApi; public class Script { public int Sum(int a, int b) { if(Host != null) Host.Log(""Sum is invoked""); return a + b; } public HostApp Host { get; set; } }") .CreateObject("*") .AlignToInterface <ICalc>(); calc.Host = host; int result = calc.Sum(1, 2); }
public static void LoadCode_WithDuckTypedInterface(HostApp host) { // 1 - LoadCode compiles code and returns instance of a first class in the compiled assembly // 2- The script class doesn't implement host app interface but it can still be aligned to // one as long at it implements the interface members // 3 - In this sample host object is passed into script routine. ICalc calc = CSScript.LoadCode(@"using CSScriptNativeApi; public class Script : ICalc { public int Sum(int a, int b) { if(Host != null) Host.Log(""Sum is invoked""); return a + b; } public HostApp Host { get; set; } }") .CreateObject("*") .AlignToInterface <ICalc>(); calc.Host = host; int result = calc.Sum(1, 2); }
static void Main(string[] args) { Program p = new Program(); p.Sum(1, 2); ICalc i = p; i.Sum(1, 2); }
static void Hybrid_InterfaceAlignment_Test() { string className = "Script"; string code = GetCode(className, ""); ICalc script = CSScript.Evaluator.LoadCode <ICalc>(code); int result = script.Sum(1, 2); }
public void LoadFile_T() { var script = GetTempScript(nameof(LoadFile), @"using System; public class Calc : Testing.ICalc { public int Sum(int a, int b) => a+b; }"); ICalc calc = evaluator.LoadFile <ICalc>(script); int result = calc.Sum(1, 2); Assert.Equal(3, result); }
public async void LoadCodeAsync() { ICalc calc = await CSScript.Evaluator .LoadCodeAsync <ICalc>( @"using System; public class Script { public int Sum(int a, int b) { return a+b; } }"); Console.WriteLine(" End of {0}: {1}", nameof(LoadCodeAsync), calc.Sum(1, 2)); }
void CalcTest_InterfaceAlignment() { ICalc calc = CSScript.Evaluator .LoadMethod <ICalc>(@"public int Sum(int a, int b) { if(Host != null) Host.Log(""Sum is invoked""); return a + b; } public HostApp Host { get; set; }"); calc.Host = this; int result = calc.Sum(1, 2); }
public void LoadCode_T() { ICalc calc = evaluator.LoadCode <ICalc>(@"using System; public class Script : Testing.ICalc { public int Sum(int a, int b) { return a+b; } }"); int result = calc.Sum(1, 2); Assert.Equal(3, result); }
public async void LoadCodeAsync() { ICalc calc = await CSScript.Evaluator .LoadCodeAsync <ICalc>( @"using System; public class Script { public int Sum(int a, int b) { return a+b; } }"); Assert.Equal(3, calc.Sum(1, 2)); }
public void LoadCodeTyped() { ICalc script = CSScript.RoslynEvaluator .LoadCode <ICalc>(@"using System; public class Script { public int Sum(int a, int b) { return a+b; } }"); int result = script.Sum(1, 2); Assert.Equal(3, result); }
public static void CalcTest_InterfaceInheritance(HostApp host) { ICalc calc = (ICalc)CSScript.LoadCode(@"public class Script : ICalc { public int Sum(int a, int b) { if(Host != null) Host.Log(""Sum is invoked""); return a + b; } public HostApp Host { get; set; } }") .CreateObject("*"); calc.Host = host; int result = calc.Sum(1, 2); }
void CalcTest_InterfaceInheritance() { ICalc calc = (ICalc)CSScript.Evaluator .LoadCode(@"public class Script : ICalc { public int Sum(int a, int b) { if(Host != null) Host.Log(""Sum is invoked""); return a + b; } public HostApp Host { get; set; } }"); calc.Host = this; int result = calc.Sum(1, 2); }
public void LoadMethodWithInterface() { // LoadMethod compiles code and returns instance of a first class // in the compiled assembly. // LoadMethod is essentially the same as LoadCode. It just deals not with the // whole class definition but a single method(s) only. And the rest of the class definition is // added automatically by CS-Script. The auto-generated class declaration also indicates // that the class implements ICalc interface. Meaning that it will trigger compile error // if the set of methods in the script code doesn't implement all interface members. ICalc script = CSScript.Evaluator .LoadMethod <ICalc>( @"int Sum(int a, int b) { return a+b; }"); int result = script.Sum(1, 2); }
public void LoadCode_WithDuckTypedInterface() { // 1 - LoadCode compiles code and returns instance of a first class in the compiled assembly // 2- The script class doesn't implement host app interface but it can still be aligned to // one as long at it implements the interface members ICalc script = CSScript.MonoEvaluator .LoadCode <ICalc>(@"using System; public class Script { public int Sum(int a, int b) { return a+b; } }"); int result = script.Sum(1, 2); }
public async void LoadCodeAsync() { //This use-case uses Interface Alignment and this requires all assemblies involved to have //non-empty Assembly.Location CSScript.GlobalSettings.InMemoryAssembly = false; ICalc calc = await CSScript.Evaluator .LoadCodeAsync <ICalc>( @"using System; public class Script { public int Sum(int a, int b) { return a+b; } }"); Console.WriteLine(" End of {0}: {1}", nameof(LoadCodeAsync), calc.Sum(1, 2)); }
public void LoadMethodTyped() { ICalc script = CSScript.RoslynEvaluator .LoadMethod <ICalc>(@"using System; public int Sum(int a, int b) { return sumImpl(a,b); } int sumImpl(int a, int b) { return a+b; }"); int result = script.Sum(1, 2); Assert.Equal(3, result); }
public void LoadCodeTyped() { //This use-case uses Interface Alignment and this requires all assemblies involved to have non-empty Assembly.Location CSScript.GlobalSettings.InMemoryAssembly = false; ICalc script = CSScript.CodeDomEvaluator .LoadCode <ICalc>(@"using System; public class Script { public int Sum(int a, int b) { return a+b; } }"); int result = script.Sum(1, 2); Assert.Equal(3, result); }
public void LoadCodeTyped() { //some of the accumulated ref assemblies are causing Mono //to throw "error CS0433: The imported type `System.MarshalByRefObject' is defined multiple times" ((result of batch UnitTesting)) //so use fresh copy (clone) of evaluator ICalc script = CSScript.MonoEvaluator .Clone(copyRefAssemblies: false) .LoadCode <ICalc>(@"using System; public class Script { public int Sum(int a, int b) { return a+b; } }"); int result = script.Sum(1, 2); Assert.Equal(3, result); }
public void LoadCode_WithDuckTypedInterface() { // 1 - LoadCode compiles code and returns instance of a first class in the compiled assembly // 2- The script class doesn't implement host app interface but it can still be aligned to // one as long at it implements the interface members //This use-case uses Interface Alignment and this requires all assemblies involved to have //non-empty Assembly.Location CSScript.GlobalSettings.InMemoryAssembly = false; ICalc script = CSScript.MonoEvaluator .LoadCode <ICalc>(@"using System; public class Script { public int Sum(int a, int b) { return a+b; } }"); int result = script.Sum(1, 2); }
void LoadCode_With_Interface_Alignment() { //NOTE: the scrip class defined in 'code' does not inherit from ICalc ICalc calc = CSScript.Evaluator.LoadCode <ICalc>(code); int result = calc.Sum(1, 2); }