silverlight EventHandlerList could not be found

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • a82720
    New Member
    • Sep 2013
    • 3

    silverlight EventHandlerList could not be found

    It's easy to obtain the values for the properties and fields or invoke methods but it has been difficult for me to get the delegate from an EventInfo.

    I have tried following the instructions from this website:


    This works to display the events in WINDOWS FORMS

    Code:
    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 'EVENTHANDERLIS T'

    Any ideas for how I can display the event VALUES in Silverlight?


    I also tried this.

    Code:
    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;
                }
            }
        }
    }
    But I get the error "FieldAccessExc eption was unhandled by user code' and ''SilverlightAp p1.MainPage.but ton1_Click (System.Object, System.Windows. RoutedEventArgs )' method 'System.Windows .Controls.Primi tives.ButtonBas e._isLoaded' failed to access the field." at

    Code:
     EventHandler eh = (EventHandler)fi.GetValue(sender);
Working...