示例#1
0
        public static void TestingChain()
        {
            DelegateChain dc1 = new DelegateChain(1.5);
            DelegateChain dc2 = new DelegateChain(2.5);

            ProcessResults[] delegates =
            {
                dc1.Compute,
                dc2.Compute,
                DelegateChain.StaticCompute,
            };

            ProcessResults cheined = delegates[0] + delegates[1] + delegates[2];

            Delegate[] chain = cheined.GetInvocationList();
            double     acc   = 0;

            for (var i = 0; i < chain.Length; i++)
            {
                ProcessResults current = (ProcessResults)chain[i];
                acc += current(4, 5);
            }
            Console.WriteLine($"Accumulator: {acc}");

            double combined = cheined(4, 5);

            Console.WriteLine($"Combined: {combined}");
        }
示例#2
0
        private static void Practice1()
        {
            Console.WriteLine("예제1");
            DelegateChain delegate1;

            delegate1  = new DelegateChain(Func1);
            delegate1 += Func2;
            delegate1();
        }
示例#3
0
        static void Main(string[] args)
        {
            int a = 5;
            int?b = null;
            int?c = null;
            int?e = 5;

            Console.WriteLine(b.Equals(a)); //false - Свойство HasValue для b равно false, а параметр метода Equals(a) не равен null
            Console.WriteLine(b.Equals(c)); //true - Свойству HasValue задано значение false, а параметру — null
                                            //(то есть два нулевых значения равны по определению).
            Console.WriteLine(b.Equals(e)); //false - свойство HasValue имеет значение true e и false для b, а значение,
                                            //возвращаемое свойством Value, не равно null.
            Console.WriteLine(e.Equals(a)); //true - свойство HasValue имеет значение true, а значение,
                                            //возвращаемое свойством Value, равно значению параметра.

            Console.WriteLine("Цепочка делегатов");
            DelegateChain delegateChain = Func1;

            delegateChain += Func2;
            delegateChain += Func3;
            delegateChain();

            Console.WriteLine("Обработка исключения в цепочке");
            DelegateChain delegateChain1;

            delegateChain1  = Func1;
            delegateChain1 += Func4;
            delegateChain1 += Func2;
            Delegate[] delegateChain1Array = delegateChain1.GetInvocationList();
            foreach (DelegateChain d in delegateChain1Array)
            {
                try
                {
                    d();
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception.Message);
                }
            }

            Console.WriteLine("Пример обобщенного делегата");
            Func <int, int, int> action;
            Action <int>         action2;

            action  = Add;
            action2 = PrintResult;
            Compare(100, 1, action, action2);
            action = Subtract;
            Compare(100, 1, action, action2);

            Console.WriteLine("Калькулятор с двумя методами: вычитание и сравнение");
            Calculator calculator = new Calculator();

            Console.WriteLine($"100-1 = {calculator.Subtract(100, 1)}");
            Console.WriteLine($"100-null = {calculator.Subtract(100, null)}");
            Console.WriteLine($"100>1 = {calculator.Compare(100,1)}");


            Type t = typeof(Calculator);

            object[] attrs = t.GetCustomAttributes(false);
            foreach (var attr in attrs)
            {
                Console.WriteLine(attr);
            }
        }