static void Run3() { var code = @"public static void Hello(string greeting) { SayHello(greeting); } static void SayHello(string greeting) { Console.WriteLine(greeting); }"; var script = new AsmHelper(CSScript.LoadMethod(code)); var SayHello = script.GetStaticMethod("Scripting.DynamicClass.Hello", ""); //fully qualified Hello method with with FastMethodInvoker SayHello("Hello World!"); }
static void Run2() { var code = @"public static void Hello(string greeting) { SayHello(greeting); } static void SayHello(string greeting) { Console.WriteLine(greeting); }"; var script = new AsmHelper(CSScript.LoadMethod(code)); var SayHello = script.GetStaticMethod("*.Hello", ""); //call first Hello method found with FastMethodInvoker SayHello("Hello World!"); }
static void Run4() { var code = @"public static void Hello(string greeting) { Console.WriteLine(greeting); }"; var script = new AsmHelper(CSScript.LoadMethod(code)); var SayHello1 = script.GetStaticMethod("*.*"); //first method found with FastMethodInvoker SayHello1("Hello World!"); //or var SayHello2 = script.GetStaticMethod(); SayHello2("Hello World!"); }
static void Main() { string code = @"using System; public class Script { public static void SayHello(string greeting) { Console.WriteLine(""Static: "" + greeting); } public void Say(string greeting) { Console.WriteLine(""Instance: "" + greeting); } }"; var script = new AsmHelper(CSScript.LoadCode(code, null, true)); //call static method script.Invoke("*.SayHello", "Hello World! (invoke)"); //call static method via emitted FastMethodInvoker var SayHello = script.GetStaticMethod("*.SayHello", ""); SayHello("Hello World! (emitted method)"); object obj = script.CreateObject("Script"); //call instance method script.InvokeInst(obj, "*.Say", "Hello World! (invoke)"); //call instance method via emitted FastMethodInvoker var Say = script.GetMethod(obj, "*.Say", ""); Say("Hello World! (emitted method)"); //call using C# 4.0 Dynamics dynamic script1 = CSScript.LoadCode(code) .CreateObject("*"); script1.Say("Hello World! (dynamic object)"); //call using CS-Scrit Interface Alignment IScript script2 = obj.AlignToInterface<IScript>(); script2.Say("Hello World! (aligned interface)"); }
static void CallStaticMethodOfFreeStandingClasslessCode() { //There is no script. //The code consist of the only method definition. //Autoclass is injected by CS-Script engine and user has no control //on class name, which is always Scripting.DynamicClass var assembly = CSScript.LoadMethod( @"public static void SayHello(string greeting) { Console.WriteLine(greeting); }"); AsmHelper script = new AsmHelper(assembly); script.Invoke("*.SayHello", "Hello World!"); //or var SayHello = script.GetStaticMethod(); SayHello("Hello World!"); }
static void TestMethodDelegates() { Assembly assembly = CSScript.LoadCode( @"using System; public class Calculator { static public void PrintSum(int a, int b) { Console.WriteLine(a + b); } public int Multiply(int a, int b) { return (a * b); } }"); AsmHelper calc = new AsmHelper(assembly); //using static method delegate var PrintSum = calc.GetStaticMethod("Calculator.PrintSum", 0, 0); PrintSum(1, 2); //using instance method delegate var obj = calc.CreateObject("Calculator"); var Multiply = calc.GetMethod(obj, "Multiply", 0, 0); Console.WriteLine(Multiply(3, 5)); //using general method delegate; can invoke both static and instance methods var methodInvoker = calc.GetMethodInvoker("Calculator.PrintSum", 0, 0); methodInvoker(null, 5, 12); }