silverlight EventHandlerList could not be found
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: silverlight EventHandlerList could not be found

  1. #1
    Join Date
    Aug 2013
    Posts
    3

    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?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,247

    Re: silverlight EventHandlerList could not be found

    What is 'EventHandlerList' and where is it defined in your code snippet?

  3. #3
    Join Date
    Aug 2013
    Posts
    3

    Re: silverlight EventHandlerList could not be found

    Quote Originally Posted by Arjay View Post
    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

  4. #4
    Join Date
    Aug 2013
    Posts
    3

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center