473,405 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

silverlight EventHandlerList could not be found

3
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
This works to display the events in WINDOWS FORMS

Expand|Select|Wrap|Line Numbers
  1. namespace WindowsFormsApplication3
  2. {
  3.     public partial class Form1 : Form
  4.     {
  5.         public Form1()
  6.         {
  7.             InitializeComponent();
  8.         }
  9.  
  10.         public Delegate[] GetEventSubscribers(object target, string eventName)
  11.         {
  12.             string WinFormsEventName = "Event" + eventName;
  13.             Type t = target.GetType();          
  14.             do
  15.             {
  16.                 FieldInfo[] fia = t.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic);
  17.                 foreach (FieldInfo fi in fia)
  18.                 {
  19.                     if (fi.Name == eventName)
  20.                     {
  21.                         //we've found the compiler generated event
  22.                         Delegate d = fi.GetValue(target) as Delegate;
  23.                         if (d != null)
  24.                             return d.GetInvocationList();
  25.                     }
  26.                     if (fi.Name == WinFormsEventName)
  27.                     {
  28.                         //we've found an EventHandlerList key
  29.                         //get the list
  30.                         EventHandlerList ehl = (EventHandlerList)target.GetType().GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy).GetValue(target, null);
  31.                         //and dereference the delegate.
  32.                         Delegate d = ehl[fi.GetValue(target)];
  33.                         if (d != null)
  34.                             return d.GetInvocationList();
  35.                     }
  36.                 }
  37.                 t = t.BaseType;
  38.             } while (t != null);
  39.  
  40.             return new Delegate[] { };
  41.         }
  42.  
  43.         private void button1_Click(object sender, EventArgs e)
  44.         {
  45.             System.Reflection.EventInfo[] eventInfo = button1.GetType().GetEvents();
  46.             for (int i = 0; i < eventInfo.Length; i++)
  47.             {
  48.                 textBox1.Text = GetEventSubscribers(button1, eventInfo[i].Name).Length.ToString() + "    " + textBox1.Text;
  49.                 Delegate[] dels = GetEventSubscribers(button1, eventInfo[i].Name);
  50.                 string text = string.Join(", ", dels.Select(d => d.Method.Name));
  51.                 textBox2.Text = text + ". " + textBox2.Text;
  52.             }
  53.         }      
  54.     }
  55. }

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 also tried this.

Expand|Select|Wrap|Line Numbers
  1. namespace eventhandlers
  2. {
  3.     public partial class MainPage : UserControl
  4.     {
  5.         public MainPage()
  6.         {
  7.             InitializeComponent();
  8.         }
  9.  
  10.         private void button1_Click(object sender, RoutedEventArgs e)
  11.         {
  12.             Type t = sender.GetType();
  13.             FieldInfo fi = t.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic);
  14.  
  15.             EventHandler eh = (EventHandler)fi.GetValue(sender);
  16.  
  17.             System.Reflection.EventInfo[] eventInfo = button1.GetType().GetEvents();                
  18.             Delegate[] dels = eh.GetInvocationList();
  19.             foreach(Delegate del in eh.GetInvocationList() )
  20.             {
  21.                 string text = del.Method.Name;
  22.                 textBlock1.Text = text + ". " + textBlock1.Text;
  23.             }
  24.         }
  25.     }
  26. }
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

Expand|Select|Wrap|Line Numbers
  1.  EventHandler eh = (EventHandler)fi.GetValue(sender);
Sep 3 '13 #1
0 1224

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Anders Borum | last post by:
Hello! This post relates to the implementation of the EventHandlerList class. When implementing custom events in a framework I'm developing, a handling of larger amounts of events in a class...
4
by: DeveloperX | last post by:
I'm having a play with EventHandlerList but the documentation is a bit ropey and I can't find any decent examples. It also doesn't seem to do what I was led to believe it would. I was under the...
2
by: Vinnie | last post by:
ok, thanks for the help. Some of you, was right: too many words, to much marketing without identifying the final user. Smithers gave the easiest answer. Now, as i wasn't enough confused, on the...
3
by: Abbas | last post by:
Does anyone know how to utilize a C# class within a silverlight project? I was trying to add a reference to my non-silverlight Class Library and the VS 2008 IDE told me "You can only add project...
14
by: Lloyd Sheen | last post by:
I have attempted several times to install this. I put this on this forum since if I go to MS site and use search it cannot find Silverlight but I have noticed several people on this forum mention...
4
by: Ken Fine | last post by:
I know about this: http://silverlight.net/forums/14.aspx and this: http://forums.asp.net/1127.aspx Silverlight Beta 2 was announced at TechEd. I was impressed. When does Silverlight get a...
1
by: Faisal Shafiq | last post by:
I want to upload a file direct to the Silverlight Streaming Service from a Web Client such as silverlight application. As per our product requirement we want to upload a .WMV file directly from...
7
Curtis Rutland
by: Curtis Rutland | last post by:
Building A Silverlight (2.0) Multi-File Uploader All source code is C#. VB.NET source is coming soon. Note: This project requires Visual Studio 2008 SP1 or Visual Web Developer 2008 SP1 and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.