Beispiel #1
0
    static void CallInstanceMethod2()
    {
        Assembly assembly = CSScript.LoadMethod(
            @"public void SayHello(string greeting)
              {
                  Console.WriteLine(greeting);
              }");

        AsmHelper script = new AsmHelper(assembly);

        object instance = script.CreateObject("*"); //wild card * can be used as the assembly contains only one class defined
        script.InvokeInst(instance, "*.SayHello", "Hello World!");
    }
Beispiel #2
0
    static void CallInstanceMethod1()
    {
        //all classless scripts belong to DynamicClass type generated by the CS-Script engine
        Assembly assembly = CSScript.LoadMethod(
            @"public void SayHello(string greeting)
              {
                  Console.WriteLine(greeting);
              }");

        AsmHelper script = new AsmHelper(assembly);

        object instance = script.CreateObject("Scripting.DynamicClass"); // or script.CreateObject("*")
        script.InvokeInst(instance, "Scripting.DynamicClass.SayHello", "Hello World!");
    }
Beispiel #3
0
    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)");
    }
Beispiel #4
0
    static void TestReflection(int numOfLoops, AsmHelper script, object instance)
    {
        //Starting from version v2.2 pure Reflection calls (script.CachingEnabled = false)
        //are ~2 times faster simple because of internal optimization in AsmHelper.
        script.CachingEnabled = false;

        Stopwatch sw = new Stopwatch();
        sw.Start();

        for (int i = 0; i < numOfLoops; i++)
            script.InvokeInst(instance, "Calculator.Add", 1, 2);

        sw.Stop();
        Console.WriteLine("Reflection: " + sw.ElapsedMilliseconds);
    }
Beispiel #5
0
    static void TestFastInvoking(int numOfLoops, AsmHelper script, object instance)
    {
        //Starting from version v2.2 pure Reflection calls are no longer the only available
        //option for invoking scrips methods. AsmHelper can cache dynamically emitted method
        //invokers and use them internally when AsmHelper's Invoke()/InvokeInst() called.
        //
        //Thus Invoke()/InvokeInst() are more than 100 times faster in v2.2 than in v2.1 when
        //AsmHelper caching is enabled (script.CachingEnabled = true).

        script.CachingEnabled = true; //it is true by default

        Stopwatch sw = new Stopwatch();
        sw.Start();

        for (int i = 0; i < numOfLoops; i++)
            script.InvokeInst(instance, "Calculator.Add", 1, 2);

        sw.Stop();
        Console.WriteLine("Fast invoking: " + sw.ElapsedMilliseconds);
    }
Beispiel #6
0
 static void TypeUnsafeReflection(AsmHelper script, object instance)
 {
     script.InvokeInst(instance, "Claculator.Sum", 1, 2);
     script.InvokeInst(instance, "Claculator.Sum", 4, 7);
 }