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/eventsubscriber s.aspx
This works to display the events in WINDOWS FORMS
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 have tried following the instructions from this website:
http://bobpowell.net/eventsubscriber s.aspx
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?
Comment