0

I'm aware that to display the Control properties, you just need to use a code like this

System.Reflection.PropertyInfo[] propertyInfo = button1.GetType().GetProperties();

for (int i = 0; i < propertyInfo.Length; i++)
{
     textBlock.Text = i + " " + propertyInfo[i].Name + "\n" + textBlock.Text;
};

You can also get the names of the Events like this

System.Reflection.EventInfo[] eventInfo = button1.GetType().GetEvents();

for (int i = 0; i < eventInfo.Length; i++)
{
   textBlock.Text = eventInfo[i].Name + "\n" + textBlock.Text;
};

But how about to display the Event handlers? I can't just use a GetValue function like I can for propertyInfo

propertyInfo[i].GetValue(button1, null)

If I try below, I get the error "System.Reflection.EventInfo does not contain a definition for GetValue"

eventInfo[i].GetValue(button1, null)

if I try the following, I only get "???" displayed. I'm unable to display "button1_click"

for (int i = 0; i < eventInfo.Length; ++i) 
{ 
      FieldInfo fi = senderType.GetField(eventInfo[i].Name, BindingFlags.NonPublic | BindingFlags.Instance); 
      if (!Object.ReferenceEquals(null, fi)) 
      { 
        tb4.Text = fi.GetValue(null) + "\n" + tb4.Text; continue; 
      } 
      PropertyInfo pi = senderType.GetProperty(eventInfo[i].Name, BindingFlags.NonPublic | BindingFlags.Instance); 
      if (!Object.ReferenceEquals(null, pi)) 
      { 
           tb4.Text = pi.GetValue(null) + "\n" + tb4.Text; continue; 
      } 
      tb4.Text = "???" + "\n" + tb4.Text; 
}

Another possible solution I've heard about is to use 'EventHandlerList'. However, if I use it in this Silverlight program, I get the error "The type or namespace name 'EventHandlerList' could not be found (are you missing a using directive or an assembly reference?)". What can I do about this?

1
Contributor
1
Reply
29
Views
3 Years
Discussion Span
Last Post by g1161018
0

No one knows? I also tried this

namespace SilverlightApp1
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, RoutedEventArgs e)
            {
                Type t = sender.GetType();
                FieldInfo[] fia = t.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic);
                foreach (FieldInfo fi in fia)
                {
                    EventHandler eh = fi.GetValue(sender) as EventHandler;
                    System.Reflection.EventInfo[] eventInfo = button1.GetType().GetEvents();                
                    Delegate[] del = eh.GetInvocationList();
                    {
                        string text = string.Join(", ", del.Select(d => d.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._is Loaded' failed to access the field." at

EventHandler eh = (EventHandler)fi.GetValue(sender);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.