3

It's easy to obtain the values for the properties and fields or invoke methods but it has been difficult for me to display the Event handlers. I can't just use a GetValue function like I can for PropertyInfo.

I have tried following the instructions from https://web.archive.org/web/20131213074318/http://bobpowell.net/eventsubscribers.aspx - this works to display the events in Windows Forms.

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 have tried this

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 "FieldAccessException was unhandled by user code' and ''SilverlightApp1.MainPage.button1_Click (System.Object, System.Windows.RoutedEventArgs)' method 'System.Windows.Controls.Primitives.ButtonBase._isLoaded' failed to access the field." at

EventHandler eh = (EventHandler)fi.GetValue(sender);
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
user2702279
  • 31
  • 1
  • 3
  • possible duplicate of [Is it possible to "steal" an event handler from one control and give it to another?](http://stackoverflow.com/questions/293007/is-it-possible-to-steal-an-event-handler-from-one-control-and-give-it-to-anoth) – Hans Passant Aug 21 '13 at 10:17
  • no, this is not a duplicate of Is it possible to "steal" an event handler from one control and give it to another? – user2702279 Sep 04 '13 at 06:20
  • In Silverlight, you cannot use reflection to access private types and members. This limitation is created for security reasons. http://msdn.microsoft.com/en-us/library/stfy7tfc(VS.95).aspx – termit Sep 05 '13 at 14:55
  • Thanks for the reply. So that must be why it's easy to obtain the values for the properties and fields or invoke methods but it has been difficult for me to display the Event handlers – user2702279 Sep 06 '13 at 01:30
  • Some of this code is invalid `FieldInfo fi = t.GetFields()` will not compile. It needs to be `FieldInfo[] fi = t.GetFields()` – Arvo Bowen May 17 '23 at 16:58

0 Answers0