Exemplo n.º 1
0
        public void AddHandler_MultipleInSameKey_Getter_CombinedDelegates()
        {
            var list = new EventHandlerList();

            // Create two delegates that will increase total by different amounts
            int total = 0;
            Action a1 = () => total += 1;
            Action a2 = () => total += 2;

            // Add both delegates for the same key and make sure we get them both out of the indexer
            list.AddHandler("key1", a1);
            list.AddHandler("key1", a2);
            list["key1"].DynamicInvoke();
            Assert.Equal(3, total);

            // Remove the first delegate and make sure the second delegate can still be retrieved
            list.RemoveHandler("key1", a1);
            list["key1"].DynamicInvoke();
            Assert.Equal(5, total);

            // Remove a delegate that was never in the list; nop
            list.RemoveHandler("key1", new Action(() => { }));
            list["key1"].DynamicInvoke();
            Assert.Equal(7, total);

            // Then remove the second delegate
            list.RemoveHandler("key1", a2);
            Assert.Null(list["key1"]);
        }
Exemplo n.º 2
0
        public void AddHandler_Getter_RemoveHandler_Getter_Roundtrips()
        {
            var list = new EventHandlerList();

            // Create two different delegate instances
            Action a1 = () => Assert.True(false);
            Action a2 = () => Assert.False(true);
            Assert.NotSame(a1, a2);

            // Neither entry in the list has a delegate
            Assert.Null(list["key1"]);
            Assert.Null(list["key2"]);

            // Add the first delegate to the first entry
            list.AddHandler("key1", a1);
            Assert.Same(a1, list["key1"]);
            Assert.Null(list["key2"]);

            // Add the second delegate to the second entry
            list.AddHandler("key2", a2);
            Assert.Same(a1, list["key1"]);
            Assert.Same(a2, list["key2"]);

            // Then remove the first delegate
            list.RemoveHandler("key1", a1);
            Assert.Null(list["key1"]);
            Assert.Same(a2, list["key2"]);

            // And remove the second delegate
            list.RemoveHandler("key2", a2);
            Assert.Null(list["key1"]);
            Assert.Null(list["key2"]);
        }
Exemplo n.º 3
0
        private void GenerateCards()
        {
            YourScore.Show();
            YourScore.Text = "Your Score: 0";
            OpponentsScore.Show();
            OpponentsScore.Text = "Opponent's Score: 0";
            _opponentPic        = new PictureBox
            {
                Name     = "Opponent's Card",
                Image    = Resources.card_back_blue,
                Location = new Point(Width - 125, 10),
                Size     = new Size(100, 114),
                SizeMode = PictureBoxSizeMode.StretchImage
            };
            Controls.Add(_opponentPic);
            Point nextLocation = new Point(10, 134);

            for (int i = 0; i < 10; i++)
            {
                PictureBox currentPic = new PictureBox
                {
                    Name     = "picDynamic" + i,
                    Image    = Resources.card_back_red,
                    Location = nextLocation,
                    Size     = new Size(100, 114),
                    SizeMode = PictureBoxSizeMode.StretchImage
                };
                currentPic.Click += (sender, e) => {
                    if (!string.IsNullOrEmpty(_drawnCard))
                    {
                        return;
                    }
                    // Generate a random card
                    _drawnCard       = RandomCard();
                    currentPic.Name  = _drawnCard;
                    currentPic.Image = (Image)Resources.ResourceManager.GetObject(FileNameByCard(currentPic.Name));
                    _amountOfOpenedCards++;
                    // Send the Generated card
                    BeginSend("1" + currentPic.Name);
                    // Receive an answer
                    BeginRead();
                    // Remove click event handler
                    FieldInfo currentPicFieldInfo = typeof(Control).GetField("EventClick",
                                                                             BindingFlags.Static | BindingFlags.NonPublic);
                    object       obj = currentPicFieldInfo?.GetValue(currentPic);
                    PropertyInfo currentPicPropertyInfo = currentPic.GetType().GetProperty("Events",
                                                                                           BindingFlags.NonPublic | BindingFlags.Instance);
                    EventHandlerList list = (EventHandlerList)currentPicPropertyInfo?.GetValue(currentPic, null);
                    list?.RemoveHandler(obj, list[obj]);
                };
                Controls.Add(currentPic);
                nextLocation.X += currentPic.Size.Width + 10;
                MainButton.Text = "Forfiet";
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 移除按钮点击事件
        /// </summary>
        /// <param name="button">按钮</param>
        public static void RemoveClickEvent(this Button button)
        {
            FieldInfo f1 = typeof(Control).GetField("EventClick", BindingFlags.Static | BindingFlags.NonPublic);

            if (f1 == null)
            {
                return;
            }

            object       obj = f1.GetValue(button);
            PropertyInfo pi  = button.GetType().GetProperty("Events", BindingFlags.NonPublic | BindingFlags.Instance);

            if (pi != null && obj != null)
            {
                EventHandlerList list = (EventHandlerList)pi.GetValue(button, null);
                list?.RemoveHandler(obj, list[obj]);
            }
        }
Exemplo n.º 5
0
 public void RemoveHandler_EmptyList_Nop()
 {
     var list = new EventHandlerList();
     list.RemoveHandler("key1", new Action(() => { })); // no error
 }