Quantcast
Viewing all articles
Browse latest Browse all 12583

Get subscribed events in a button

Hi people... recently I wrote this code, to try get subscribed events in controls by reflection:

class AnyControl    {        public event EventHandler Event1;        public event EventHandler Event2;        public event EventHandler Event3;    }    internal class Program    {        private static void Main(string[] args)        {            var control = new AnyControl();            control.Event2 += (sender, argsButton) => { Console.WriteLine("hi!"); };            var tipo = typeof (AnyControl);            var possibleEvents = tipo.GetEvents();            foreach (var possibleEvent in possibleEvents)            {                var field = tipo.GetField(possibleEvent.Name, BindingFlags.NonPublic | BindingFlags.Instance);                if (field == null)                    continue;                var handler = (Delegate) field.GetValue(control);                if (handler == null)                    continue;                if (handler.GetInvocationList().Count() == 0)                    continue;                Console.WriteLine(possibleEvent.Name);            }            Console.WriteLine("end!");            Console.ReadLine();            return;        }    }

But, when I tried to test it with Button control, the code didn't work!

Someone have any idea why it happens!?


Viewing all articles
Browse latest Browse all 12583

Trending Articles