コード例 #1
0
        public void Run()
        {
            Console.WriteLine("----------------------------------------");
            Console.WriteLine("Running " + GetType().Name + "Demo");
            Console.WriteLine("----------------------------------------");

            MySimpleDelegate f1 = () => 4;
            MySimpleDelegate f2 = () => { return(4); };               // Statement Lambda

            MyDelegate f3 = s => { return(s.Length); };               // Statement Lambda with arguments
            MyDelegate f4 = (s) => { return(s.Length); };
            MyDelegate f5 = (string s) => { return(s.Length); };

            MyDelegate2 f6 = (string s, int i) => { return(s.Length + i); };

            Console.WriteLine("From function f1 : " + f1());
            Console.WriteLine("From function f2 : " + f2());

            Console.WriteLine("From function f3 : " + f3("Hello world 1"));
            Console.WriteLine("From function f4 : " + f4("Hello world 22"));
            Console.WriteLine("From function f5 : " + f5("Hello world 333"));

            Console.WriteLine("From function f6 : " + f6("Hello world 444", 5));

            MyDelegate f7 = GetNewDelegate(); // Google stackoverflow : What are Closures in .NET?

            Console.WriteLine("From function f7 : " + f7("Hello world 5555"));

            Console.WriteLine("----------------------------------------");
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: mkejeiri/csharp-Samples
        static void Main(string[] args)
        {
            //0 - Calling a func
            SayHello();

            //1- traditional way of using delegate: a class which encapsulates a function
            MySimpleDelegate simpleDel = new MySimpleDelegate(SayHello);

            simpleDel.Invoke();

            //2-Sugar or shorthand syntax
            MySimpleDelegate sugarSyntaxDel = SayHello;

            sugarSyntaxDel();

            //3- Passing a function as delegate
            Test(SayHello);

            //4- Sugar or shorthand syntax
            MyDelegate sugarSyntaxDelWithParams = SayHelloWithParams;

            sugarSyntaxDelWithParams("Kejeiri");

            //5- Delegate with params, passing a function with params
            TestWithParams(SayHelloWithParams);

            //6- Using a function to create a delegate
            MyDelegate DelWithParams = GiveMeMyDelegate();

            DelWithParams("Kejeiri: GiveMeMyDelegate");
            Console.ReadKey();
        }
コード例 #3
0
ファイル: interoptest.cs プロジェクト: SSCLI/sscli_20021101
 public MySimpleDelegate CreateSimpleCallback(bool cache)
 {
   MySimpleDelegate callback = new MySimpleDelegate(this.MySimpleCallback);
   if (cache) {
       CacheSimpleDelegate(callback);
       _simpleDelegate = callback;
       callback = null;
   }
   return callback;
 }
コード例 #4
0
ファイル: Program.cs プロジェクト: mkejeiri/csharp-Samples
 static void Test(MySimpleDelegate del)
 {
     del();
 }
コード例 #5
0
 static extern MySimpleDelegate DelegateMarshal(MySimpleDelegate dlg);
コード例 #6
0
 static extern int CallSimpleDelegate(MySimpleDelegate pfn, int x);
コード例 #7
0
 static extern int CacheSimpleDelegate(MySimpleDelegate pfn);
コード例 #8
0
    public static int Main()
    {
        MainApp a = new MainApp();

        // simple delegate roundtrip
        {
            int x = CallSimpleDelegate(a.CreateSimpleCallback(false), 1);
            if (x != 2)
            {
                Console.WriteLine("Unexpected value: " + x.ToString());
                return(1);
            }
        }
        Console.WriteLine("Simple delegate roundtrip passed");

        // delegate roundtrip
        {
            double x = CallDelegate(a.CreateCallback(false), new MyValue(1));
            if (x != 2)
            {
                Console.WriteLine("Unexpected value: " + x.ToString());
                return(1);
            }
        }
        Console.WriteLine("Delegate roundtrip passed");

        // simple delegate roundtrip - crossdomain
        {
            AppDomain        app       = AppDomain.CreateDomain("MyDomain");
            MainApp          remoteapp = (MainApp)app.CreateInstanceAndUnwrap("interoptest1", "MainApp");
            MySimpleDelegate callback  = remoteapp.CreateSimpleCallback(true);

            int x = CallSimpleDelegate(callback, 1);
            if (x != 2)
            {
                Console.WriteLine("Unexpected value: " + x.ToString());
                return(1);
            }
        }
        Console.WriteLine("Simple delegate roundtrip crossdomain passed");

        // delegate roundtrip - crossdomain
        {
            AppDomain  app       = AppDomain.CreateDomain("MyDomain");
            MainApp    remoteapp = (MainApp)app.CreateInstanceAndUnwrap("interoptest1", "MainApp");
            MyDelegate callback  = remoteapp.CreateCallback(true);

            double x = CallDelegate(callback, new MyValue(1));
            if (x != 2)
            {
                Console.WriteLine("Unexpected value: " + x.ToString());
                return(1);
            }
        }
        Console.WriteLine("Delegate roundtrip crossdomain passed");

        // ffi roundtrip
        {
            double d = CallFFI(a, new MyValue(1));
            if (d != 2)
            {
                Console.WriteLine("Unexpected value: " + d.ToString());
                return(2);
            }
        }
        Console.WriteLine("FFI roundtrip passed");

        // array marshaling
        {
            int[] rg = new int[4];
            rg[0] = 12;
            rg[1] = 33;
            rg[2] = -21;
            rg[3] = 18;
            int sum = Sum(rg.Length, rg);
            if (sum != 42)
            {
                Console.WriteLine("Unexpected value: " + sum.ToString());
                return(3);
            }
        }
        Console.WriteLine("Array marshalling passed");

        // array marshaling ex
        {
            MyStruct[] rg = new MyStruct[7];
            rg[0].value = new MyValue(2314);
            rg[1].value = new MyValue(3452);
            rg[2].value = new MyValue(3235);
            rg[3].value = new MyValue(3452);
            rg[4].value = new MyValue(6980);
            rg[5].value = new MyValue(3133);
            rg[6].value = new MyValue(3426);
            int sum = ObjectSum(rg.Length, rg);
            if (sum != 25992)
            {
                Console.WriteLine("Unexpected value: " + sum.ToString());
                return(4);
            }
        }
        Console.WriteLine("Array marshalling ex passed");

        // errorinfo roundtrip
        {
            bool thrown = false;

            try {
                ReallyBadError(new MyValue(0));
            }
            catch (Exception e)
            {
                string s = e.ToString();
                if (s.IndexOf("qwerty") == -1)
                {
                    Console.WriteLine("Unexpected value: " + s);
                    return(5);
                }
                thrown = true;
            }

            if (!thrown)
            {
                Console.WriteLine("Exception wasn't thrown");
                return(5);
            }
        }
        Console.WriteLine("IErrorInfo roundtrip passed");

        // delegate roundtrip
        {
            MySimpleDelegate d1 = a.CreateSimpleCallback(false);
            MySimpleDelegate d2 = DelegateMarshal(d1);
            if (d1 != d2)
            {
                Console.WriteLine("Delegate marshal failed");
                return(8);
            }
        }
        Console.WriteLine("Delegate marshal passed");

        Console.WriteLine("All test passed");
        return(0);
    }
コード例 #9
0
 static extern MySimpleDelegate DelegateMarshal(MySimpleDelegate dlg);
コード例 #10
0
 static extern int CallSimpleDelegate(MySimpleDelegate pfn, int x);
コード例 #11
0
 static extern int CacheSimpleDelegate(MySimpleDelegate pfn);
コード例 #12
0
 public MySimpleDelegate CreateSimpleCallback(bool cache)
 {
   MySimpleDelegate callback = new MySimpleDelegate(this.MySimpleCallback);
   if (cache) {
       CacheSimpleDelegate(callback);
       _simpleDelegate = callback;
       callback = null;
   }
   return callback;
 }