I have tried following the instructions from this website:
http://bobpowell.net/eventsubscribers.aspx
This works to display the events in WINDOWS FORMS
Expand|Select|Wrap|Line Numbers
- namespace WindowsFormsApplication3
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- public Delegate[] GetEventSubscribers(object target, string eventName)
- {
- string WinFormsEventName = "Event" + eventName;
- Type t = target.GetType();
- do
- {
- FieldInfo[] fia = t.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic);
- foreach (FieldInfo fi in fia)
- {
- if (fi.Name == eventName)
- {
- //we've found the compiler generated event
- Delegate d = fi.GetValue(target) as Delegate;
- if (d != null)
- return d.GetInvocationList();
- }
- if (fi.Name == WinFormsEventName)
- {
- //we've found an EventHandlerList key
- //get the list
- EventHandlerList ehl = (EventHandlerList)target.GetType().GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy).GetValue(target, null);
- //and dereference the delegate.
- Delegate d = ehl[fi.GetValue(target)];
- if (d != null)
- return d.GetInvocationList();
- }
- }
- t = t.BaseType;
- } while (t != null);
- return new Delegate[] { };
- }
- private void button1_Click(object sender, EventArgs e)
- {
- System.Reflection.EventInfo[] eventInfo = button1.GetType().GetEvents();
- for (int i = 0; i < eventInfo.Length; i++)
- {
- textBox1.Text = GetEventSubscribers(button1, eventInfo[i].Name).Length.ToString() + " " + textBox1.Text;
- Delegate[] dels = GetEventSubscribers(button1, eventInfo[i].Name);
- string text = string.Join(", ", dels.Select(d => d.Method.Name));
- textBox2.Text = text + ". " + textBox2.Text;
- }
- }
- }
- }
However, this WILL NOT WORK FOR SILVERLIGHT because Silverlight does not have a 'EVENTHANDERLIST'
Any ideas for how I can display the event VALUES in Silverlight?
I also tried this.
Expand|Select|Wrap|Line Numbers
- namespace eventhandlers
- {
- public partial class MainPage : UserControl
- {
- public MainPage()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, RoutedEventArgs e)
- {
- Type t = sender.GetType();
- FieldInfo fi = t.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic);
- EventHandler eh = (EventHandler)fi.GetValue(sender);
- System.Reflection.EventInfo[] eventInfo = button1.GetType().GetEvents();
- Delegate[] dels = eh.GetInvocationList();
- foreach(Delegate del in eh.GetInvocationList() )
- {
- string text = del.Method.Name;
- textBlock1.Text = text + ". " + textBlock1.Text;
- }
- }
- }
- }
Expand|Select|Wrap|Line Numbers
- EventHandler eh = (EventHandler)fi.GetValue(sender);