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); }
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 Test() { var host = new HostApp(); host.Log("Testing compiling services CS-Script Native API"); Console.WriteLine("---------------------------------------------"); CodeDomSamples.LoadMethod_Instance(); CodeDomSamples.LoadMethod_Static(); CodeDomSamples.LoadDelegate(); CodeDomSamples.CreateAction(); CodeDomSamples.CreateFunc(); CodeDomSamples.LoadCode(); CodeDomSamples.LoadCode_WithInterface(host); CodeDomSamples.LoadCode_WithDuckTypedInterface(host); CodeDomSamples.ExecuteAndUnload(); //CodeDomSamples.DebugTest(); //uncomment if want to fire an assertion during the script execution }
public static void LoadCode_WithInterface(HostApp host) { // 1 - LoadCode compiles code and returns instance of a first class in the compiled assembly. // 2 - The script class implements host app interface so the returned object can be type casted into it. // 3 - In this sample host object is passed into script routine. var calc = (ICalc)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("*"); 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); }