Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Finding a custom event using reflection

Finding a custom event using reflection

Scheduled Pinned Locked Moved C#
question
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Clive D Pottinger
    wrote on last edited by
    #1

    Hello Gurus. Yes, I've painted myself into yet another corner. It seems to be what I do best, so I guess I should stick with it. This time I am trying use reflection to find an event that I have attached to a class, but reflection is not showing the event. I don't know why. I have created a simple class called ListPicker that is based on the DomainUpDown control. It is supposed to act just like a DomainUpDown control, but it supports the Modified property and the ModifiedChanged events that DomainUpDown does not. Here is the code:

    using System;
    using System.Windows.Forms;

    namespace WindowsFormsControlLibrary
    {
    public partial class ListPicker : DomainUpDown
    {
    public EventHandler ModifiedChanged;

    	public ListPicker()
    	{
    		InitializeComponent();
    
    		// Register our TextChanged hander.
    		TextChanged += new EventHandler(ListPicker\_TextChanged);
    
    		Modified = false;
    	}
    
    	public bool Modified { get; set; }
    
    	public void ListPicker\_TextChanged(object sender, EventArgs args)
    	{
    		Modified = true;
    		OnModifiedChanged();
    	}
    
    	protected void OnModifiedChanged()
    	{
    		if (ModifiedChanged != null)
    		{
    			ModifiedChanged(this, null);
    		}				
    	}
    }
    

    }

    The Modified property works just as I wanted it to, and the ModifiedChanged event fires when it is supposed to, but when I use this code (where ctl is a reference to the ListPicker control)

    var x = ctl.GetType().GetEvents();
    var modifiedChangedEventInfo = ctl.GetType().GetEvent("ModifiedChanged");

    to look for the events on the ListPicker, it does not show the ModifiedChanged event. All the other DomainUpDown event show up in x, but not the ModifiedChanged event and modifiedChangedEventInfo is null Any ideas? ... and many thanks

    Clive Pottinger Victoria, BC

    L L 2 Replies Last reply
    0
    • C Clive D Pottinger

      Hello Gurus. Yes, I've painted myself into yet another corner. It seems to be what I do best, so I guess I should stick with it. This time I am trying use reflection to find an event that I have attached to a class, but reflection is not showing the event. I don't know why. I have created a simple class called ListPicker that is based on the DomainUpDown control. It is supposed to act just like a DomainUpDown control, but it supports the Modified property and the ModifiedChanged events that DomainUpDown does not. Here is the code:

      using System;
      using System.Windows.Forms;

      namespace WindowsFormsControlLibrary
      {
      public partial class ListPicker : DomainUpDown
      {
      public EventHandler ModifiedChanged;

      	public ListPicker()
      	{
      		InitializeComponent();
      
      		// Register our TextChanged hander.
      		TextChanged += new EventHandler(ListPicker\_TextChanged);
      
      		Modified = false;
      	}
      
      	public bool Modified { get; set; }
      
      	public void ListPicker\_TextChanged(object sender, EventArgs args)
      	{
      		Modified = true;
      		OnModifiedChanged();
      	}
      
      	protected void OnModifiedChanged()
      	{
      		if (ModifiedChanged != null)
      		{
      			ModifiedChanged(this, null);
      		}				
      	}
      }
      

      }

      The Modified property works just as I wanted it to, and the ModifiedChanged event fires when it is supposed to, but when I use this code (where ctl is a reference to the ListPicker control)

      var x = ctl.GetType().GetEvents();
      var modifiedChangedEventInfo = ctl.GetType().GetEvent("ModifiedChanged");

      to look for the events on the ListPicker, it does not show the ModifiedChanged event. All the other DomainUpDown event show up in x, but not the ModifiedChanged event and modifiedChangedEventInfo is null Any ideas? ... and many thanks

      Clive Pottinger Victoria, BC

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Of what type is ctl? Is it of the class DomainUpDown or of ListPicker? If it is of DomainUpDown, you probably only see the events of this class and not of any derived classes.

      And from the clouds a mighty voice spoke:
      "Smile and be happy, for it could come worse!"

      And I smiled and was happy
      And it came worse.

      1 Reply Last reply
      0
      • C Clive D Pottinger

        Hello Gurus. Yes, I've painted myself into yet another corner. It seems to be what I do best, so I guess I should stick with it. This time I am trying use reflection to find an event that I have attached to a class, but reflection is not showing the event. I don't know why. I have created a simple class called ListPicker that is based on the DomainUpDown control. It is supposed to act just like a DomainUpDown control, but it supports the Modified property and the ModifiedChanged events that DomainUpDown does not. Here is the code:

        using System;
        using System.Windows.Forms;

        namespace WindowsFormsControlLibrary
        {
        public partial class ListPicker : DomainUpDown
        {
        public EventHandler ModifiedChanged;

        	public ListPicker()
        	{
        		InitializeComponent();
        
        		// Register our TextChanged hander.
        		TextChanged += new EventHandler(ListPicker\_TextChanged);
        
        		Modified = false;
        	}
        
        	public bool Modified { get; set; }
        
        	public void ListPicker\_TextChanged(object sender, EventArgs args)
        	{
        		Modified = true;
        		OnModifiedChanged();
        	}
        
        	protected void OnModifiedChanged()
        	{
        		if (ModifiedChanged != null)
        		{
        			ModifiedChanged(this, null);
        		}				
        	}
        }
        

        }

        The Modified property works just as I wanted it to, and the ModifiedChanged event fires when it is supposed to, but when I use this code (where ctl is a reference to the ListPicker control)

        var x = ctl.GetType().GetEvents();
        var modifiedChangedEventInfo = ctl.GetType().GetEvent("ModifiedChanged");

        to look for the events on the ListPicker, it does not show the ModifiedChanged event. All the other DomainUpDown event show up in x, but not the ModifiedChanged event and modifiedChangedEventInfo is null Any ideas? ... and many thanks

        Clive Pottinger Victoria, BC

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        that must be because it isn't an event, it is just a delegate. Add the event keyword to your declaration and it should show up. :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        L C B 3 Replies Last reply
        0
        • L Luc Pattyn

          that must be because it isn't an event, it is just a delegate. Add the event keyword to your declaration and it should show up. :)

          Luc Pattyn [My Articles] Nil Volentibus Arduum

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Good eyes! I just saw what I expected to see.

          And from the clouds a mighty voice spoke:
          "Smile and be happy, for it could come worse!"

          And I smiled and was happy
          And it came worse.

          1 Reply Last reply
          0
          • L Luc Pattyn

            that must be because it isn't an event, it is just a delegate. Add the event keyword to your declaration and it should show up. :)

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            C Offline
            C Offline
            Clive D Pottinger
            wrote on last edited by
            #5

            Whoa! :doh: I feel like I've just gone to the hospital to ask a surgeon why I keep getting headaches whenever I hit my head with a hammer. There should be a facepalm emoticon. Thanks guys. I'll apply your suggestion to the code tonight and see what happens.

            Clive Pottinger Victoria, BC

            C 1 Reply Last reply
            0
            • C Clive D Pottinger

              Whoa! :doh: I feel like I've just gone to the hospital to ask a surgeon why I keep getting headaches whenever I hit my head with a hammer. There should be a facepalm emoticon. Thanks guys. I'll apply your suggestion to the code tonight and see what happens.

              Clive Pottinger Victoria, BC

              C Offline
              C Offline
              Clive D Pottinger
              wrote on last edited by
              #6

              Yup - that was it. embarassing... that's all I have to say... embarassing oh, and thanks. BTW: I find it interesting that the question from one C.D.P was answered by another C.D.P.

              Clive Pottinger Victoria, BC

              L L 2 Replies Last reply
              0
              • C Clive D Pottinger

                Yup - that was it. embarassing... that's all I have to say... embarassing oh, and thanks. BTW: I find it interesting that the question from one C.D.P was answered by another C.D.P.

                Clive Pottinger Victoria, BC

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                Don't worry. And you're welcome. BTW: you shouldn't reply to self, as nobody gets mail notification when you do. :)

                Luc Pattyn [My Articles] Nil Volentibus Arduum

                1 Reply Last reply
                0
                • C Clive D Pottinger

                  Yup - that was it. embarassing... that's all I have to say... embarassing oh, and thanks. BTW: I find it interesting that the question from one C.D.P was answered by another C.D.P.

                  Clive Pottinger Victoria, BC

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Clive D. Pottinger wrote:

                  BTW: I find it interesting that the question from one C.D.P was answered by another C.D.P

                  In my case the 'CDP' has nothing to do with my name or initials. A CDP1802 or RCA1802[^] was the CPU in my first computer. Look closely at the assembly code sample. One of the instructions, the one to designate the stack pointer, still does not fail to bring a wide grin to programming.

                  And from the clouds a mighty voice spoke:
                  "Smile and be happy, for it could come worse!"

                  And I smiled and was happy
                  And it came worse.

                  W 1 Reply Last reply
                  0
                  • L Lost User

                    Clive D. Pottinger wrote:

                    BTW: I find it interesting that the question from one C.D.P was answered by another C.D.P

                    In my case the 'CDP' has nothing to do with my name or initials. A CDP1802 or RCA1802[^] was the CPU in my first computer. Look closely at the assembly code sample. One of the instructions, the one to designate the stack pointer, still does not fail to bring a wide grin to programming.

                    And from the clouds a mighty voice spoke:
                    "Smile and be happy, for it could come worse!"

                    And I smiled and was happy
                    And it came worse.

                    W Offline
                    W Offline
                    Wayne Gaylard
                    wrote on last edited by
                    #9

                    An instruction any man would love to comply with I think :laugh:

                    When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

                    L 1 Reply Last reply
                    0
                    • W Wayne Gaylard

                      An instruction any man would love to comply with I think :laugh:

                      When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      Yes, who does not like to set the stack pointer :)

                      And from the clouds a mighty voice spoke:
                      "Smile and be happy, for it could come worse!"

                      And I smiled and was happy
                      And it came worse.

                      1 Reply Last reply
                      0
                      • L Luc Pattyn

                        that must be because it isn't an event, it is just a delegate. Add the event keyword to your declaration and it should show up. :)

                        Luc Pattyn [My Articles] Nil Volentibus Arduum

                        B Offline
                        B Offline
                        BobJanova
                        wrote on last edited by
                        #11

                        Good catch. I read right over that.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups