예제 #1
0
파일: Hello.cs 프로젝트: Diullei/Storm
    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)");
    }
예제 #2
0
    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);
    }