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. How do I create a generic .Net event handler

How do I create a generic .Net event handler

Scheduled Pinned Locked Moved C#
csharptutorialquestionlearning
4 Posts 3 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.
  • A Offline
    A Offline
    astibich2
    wrote on last edited by
    #1

    I am attempting to write code that sinks an event with only an object reference and the event name - no type information. I write a generic event handler function: public void ComponentEventHandler(object o, EventArgs e) { // handle generic event } Then I wrote some code that attaches any event to this event handler function protected void SinkControlEvent(Control ctrl, string eventName) { EventDescriptor event = TypeDescriptor.GetEvents(ctrl)[eventName]; Delegate del = Delegate.CreateDelegate(typeof(System.EventHandler), this, "ComponentEventHandler"); event .AddEventHandler(ctrl, del); } This works fine when the event I'm sinking has the standard function signature. However, I get an exception when the event has a derived EventArgs parameter. For example, trying to sink the following event would cause a type mismatch exception: public event void ValueChanged(object sender, ValueEventArgs e); The exception is "Invalid event handler for ValueChanged event". Of course 'ValueEventArgs' inherits from EventArgs, so I don't understand why this isn't a valid handler. Some things to note: 1. You can, in code, assign an untyped event handler to a typed event. Somehow the compiler figures it out. 2. I tried changing my code such that I attach an untyped function to a typed delegate, but that also threw an exception. Delegate del = Delegate.CreateDelegate(event.EventType, this, "ComponentEventHandler"); Thanks, Aaron Stibich

    P 1 Reply Last reply
    0
    • A astibich2

      I am attempting to write code that sinks an event with only an object reference and the event name - no type information. I write a generic event handler function: public void ComponentEventHandler(object o, EventArgs e) { // handle generic event } Then I wrote some code that attaches any event to this event handler function protected void SinkControlEvent(Control ctrl, string eventName) { EventDescriptor event = TypeDescriptor.GetEvents(ctrl)[eventName]; Delegate del = Delegate.CreateDelegate(typeof(System.EventHandler), this, "ComponentEventHandler"); event .AddEventHandler(ctrl, del); } This works fine when the event I'm sinking has the standard function signature. However, I get an exception when the event has a derived EventArgs parameter. For example, trying to sink the following event would cause a type mismatch exception: public event void ValueChanged(object sender, ValueEventArgs e); The exception is "Invalid event handler for ValueChanged event". Of course 'ValueEventArgs' inherits from EventArgs, so I don't understand why this isn't a valid handler. Some things to note: 1. You can, in code, assign an untyped event handler to a typed event. Somehow the compiler figures it out. 2. I tried changing my code such that I attach an untyped function to a typed delegate, but that also threw an exception. Delegate del = Delegate.CreateDelegate(event.EventType, this, "ComponentEventHandler"); Thanks, Aaron Stibich

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      astibich2 wrote:

      public void ComponentEventHandler(object o, EventArgs e)

      Could you not change this to

      public void ComponentEventHandler<T>(object sender, T e) where T : EventArgs

      Deja View - the feeling that you've seen this post before.

      My blog | My articles | MoXAML PowerToys

      M 1 Reply Last reply
      0
      • P Pete OHanlon

        astibich2 wrote:

        public void ComponentEventHandler(object o, EventArgs e)

        Could you not change this to

        public void ComponentEventHandler<T>(object sender, T e) where T : EventArgs

        Deja View - the feeling that you've seen this post before.

        My blog | My articles | MoXAML PowerToys

        M Offline
        M Offline
        Mark Churchill
        wrote on last edited by
        #3

        That looks a lot like the EventHandler< T> class ;)

        Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
        Alpha release: Entanglar: Transparant multiplayer framework for .Net games.

        P 1 Reply Last reply
        0
        • M Mark Churchill

          That looks a lot like the EventHandler< T> class ;)

          Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
          Alpha release: Entanglar: Transparant multiplayer framework for .Net games.

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          Well, I thought so, but I can't see what he's trying to do that you can't accomplish normally.

          Deja View - the feeling that you've seen this post before.

          My blog | My articles | MoXAML PowerToys

          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