Пример #1
0
        static void Generic()
        {
            GFunc(56);
            GDelegate <float> gDelegate = GFunc;

            //NOTE this is a delegate type
            var tg = gDelegate.GetType();

            var closeFunc = gDelegate.Method;
            var openFunc  = gDelegate.Method.GetGenericMethodDefinition();

            Console.WriteLine(closeFunc.IsGenericMethod);
            Console.WriteLine(closeFunc.IsGenericMethodDefinition);

            var openList = typeof(List <>);

            Console.WriteLine(openList.IsGenericType);           //NOTE only here can use `<>`
            Console.WriteLine(openList.IsGenericTypeDefinition); //NOTE true here

            var closeList = typeof(List <long>);

            Console.WriteLine(closeList.IsGenericTypeDefinition); //NOTE false here

            //open - 还没完全实化
            //close - 所有形参都实化了
            Console.WriteLine(openList.ContainsGenericParameters);         //true
            Console.WriteLine(closeList.ContainsGenericParameters);        //false
            Console.WriteLine(gDelegate.Method.ContainsGenericParameters); //false

            //用 MakeGeneric...实化
            //取得泛化版本 typeOfList.GetGenericTypeDefinition()

            //取得实化泛型的那些 types
            var t = closeList.GetGenericArguments();                                 //NOTE type long here

            Console.WriteLine(openFunc.GetGenericArguments()[0].IsGenericParameter); //NOTE True
        }
Пример #2
0
        private static void Main(string[] args)
        {
            Print sth;

            int userValue = Convert.ToInt32(Console.ReadLine());

            if (userValue % 2 == 0)
            {
                sth = Printer.PrintEven;
            }
            else
            {
                sth = Printer.PrintOdd;
            }

            sth();

            // - - - - - - - - - -
            Console.WriteLine();

            MathOperations math = new MathOperations();

            Operation del = math.Sum;

            Console.WriteLine(del(3, 9));
            del = math.Multiply;
            Console.WriteLine(del(3, 9));

            // - - - - - - - - - -
            Console.WriteLine();

            MyDelegate myDel = ManyMethods.ReturnAsSame;

            myDel += ManyMethods.Squaring;
            myDel += ManyMethods.MinusOne;
            myDel += ManyMethods.Squaring;
            myDel += ManyMethods.Squaring;

            Console.WriteLine("Return " + myDel(5));

            Console.WriteLine(">>> New:");

            myDel -= ManyMethods.Squaring;

            Console.WriteLine("Return " + myDel(5));

            // - - - - - - - - - -
            Console.WriteLine();

            OtherDelegate firstDelegate = OtherMethods.Greet;

            firstDelegate += OtherMethods.HowAreYou;

            OtherDelegate secondDelegate = OtherMethods.WhatsUp;

            secondDelegate += OtherMethods.Bye;

            OtherDelegate generalDelegate = firstDelegate + secondDelegate;

            generalDelegate("Mike");

            // - - - - - - - - - -
            Console.WriteLine();

            Invoke_Delegate(sth);

            // - - - - - - - - - -
            Console.WriteLine();

            GDelegate <int> sqDel = Squaring;

            Console.WriteLine(sqDel(5));
        }