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:
http://bobpowell.net/eventsubscribers.aspx
and I have tried this code so far:
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
System.Reflection.EventInfo[] eventInfo = button1.GetType().GetEvents();
for (int i = 0; i < eventInfo.Length; ++i)
{
Type senderType = typeof(Form1);
FieldInfo fi = senderType.GetField(eventInfo[i].Name, BindingFlags.NonPublic | BindingFlags.Instance);
if (!Object.ReferenceEquals(null, fi))
{
textBox2.Text = fi.GetValue(null) + "\n" + textBox2.Text;
continue;
}
PropertyInfo pi = senderType.GetProperty(eventInfo[i].Name, BindingFlags.NonPublic | BindingFlags.Instance);
if (!Object.ReferenceEquals(null, pi))
{
textBox2.Text = pi.GetValue(button1, null) + "\n" + textBox2.Text;
continue;
}
EventHandlerList ehl = senderType.GetProperty("Events", BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic).GetValue(button1, null) as EventHandlerList;
Delegate del = ehl[fi.GetValue(sender)];
Delegate[] dels = del.GetInvocationList();
foreach (Delegate nextDel in del.GetInvocationList())
{
textBox2.Text = nextDel.Method.Name + "\n" + textBox2.Text;
}
}
}
}
}But I get the error "object reference not set to an instance of an object" for this part of the code
Delegate del = ehl[fi.GetValue(sender)];