-
August 27th, 2013, 09:38 PM
#1
silverlight EventHandlerList could not be found
I'm aware that to display the Control properties, you just need to use a code like this
Code:
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
Code:
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
Code:
propertyInfo[i].GetValue(button1, null)
If I try below, I get the error "System.Reflection.EventInfo does not contain a definition for GetValue"
Code:
eventInfo[i].GetValue(button1, null)
if I try the following, I only get "???" displayed. I'm unable to display "button1_click"
Code:
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;
}
I saw a possible solution on StackOverflow in this thread Is it possible to "steal" an event handler from one control and give it to another?
However, if I use 'EventHandlerList' 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?
-
August 30th, 2013, 02:50 PM
#2
Re: silverlight EventHandlerList could not be found
What is 'EventHandlerList' and where is it defined in your code snippet?
-
September 1st, 2013, 07:20 PM
#3
Re: silverlight EventHandlerList could not be found
Originally Posted by Arjay
What is 'EventHandlerList' and where is it defined in your code snippet?
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)
{
////textBox1.Text = GetEventSubscribers(sender) + " \n" + textBox1.Text;
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;
// textBox2.Text = ". " + textBox2.Text;
}
}
}
}
This is a code that I've used that can display the event handlers. It requires using 'EventHandlerList' as explained in this article
http://bobpowell.net/eventsubscribers.aspx
This code works for Windows Forms, but not for Silverlight
-
September 1st, 2013, 07:22 PM
#4
Re: silverlight EventHandlerList could not be found
so that's why I want to know if there is another method to get this to work for Silverlight, since EventHandlerList doesn't work for Silverlight. EventHandlerList is located in this part of the code
Code:
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();
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|