Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 199085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:59:35+00:00 2026-05-11T16:59:35+00:00

I had once a situation where I had to override the event handler in

  • 0

I had once a situation where I had to override the event handler in some derived class of mine. I could not unfortunately just “override” it since the implementation logic was bound strictly to that particular method implementation in the base class.

My natural idea was to first “unbind” the original event handler from the event and then to bind my own brand-new method. So I tried to use the “-=” operator like:

myObject.SomeEvent -= new EventHandler (BaseClass.SomeEventHandler)

at which point the compiler complainer that it had no access to the private method SomeEventHandler. Since the base class was a part of the library I did not really want to modify its implementation (though simply as it seems by just turning “private” into “protected”);

It’s so simple to attach an event handler to an event. Why is it so difficult to get rid of one?

Why I wanted it? Because the base implementation was casing some problems (just did not handle our particular case) so I wanted to redefine it. But even after I attached my event handler the base implementation was getting executed anyway. Since it was doing some kind of Redirect, my own implementation was never going to run since the request processing was being broken at server after redirect.

Is there anyway to get rid of an event handler in a base class from a library without modifying its implementation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:59:35+00:00Added an answer on May 11, 2026 at 4:59 pm

    It would be just as easy to get rid of the handler if your code can officially see the handler. If absolutely necessary, it is possible to unsubscribe using reflection to get hold of the delegate, but this is messy. It would be a lot cleaner and safer to either a: avoid the requirement, or b: change the code to make the handler protected.


    Reflection approach (use at own risk); uncomment the block in the Bar.ctor() to see the change.

    using System;
    class Foo
    {
        public event EventHandler SomeEvent;
        public Foo()
        {
            SomeEvent += SecretHandler; // a bad idea, anyway
                                        //(self-subscribed events....)
        }
        protected virtual void OnSomeEvent()
        {
            EventHandler handler = SomeEvent;
            if (handler != null) handler(this, EventArgs.Empty);
        }
        private void SecretHandler(object sender, EventArgs args)
        {
            Console.WriteLine("suscribed");
        }
    }
    class Bar : Foo
    {
        public Bar()
        {
            /*
            MethodInfo method = typeof(Foo).GetMethod("SecretHandler",
                BindingFlags.NonPublic | BindingFlags.Instance);
            EventHandler handler = (EventHandler)Delegate.CreateDelegate(
                typeof(EventHandler), this, method);
            SomeEvent -= handler;
             */
        }
        public void Test()
        {
            OnSomeEvent();
        }
    }
    static class Program
    {
        static void Main()
        {
            Bar bar = new Bar();
            bar.Test();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I once found a web site that had some 'Requires .net 1.0', 'Requires .net
I have a server that is running Ubuntu Linux Server Edition. I once had
Once I had a file, to see the contents I used cat command, say
I had a login page. once user successfuly logged in, they can view and
I've worked with ASP.NET once before and had a project where it was potentially
I have unfortunately wandered into a situation where I need regex using Ruby. Basically
Sorry for the lengthy post, I just want to illustrate my situation as best
I'm pretty rusty in my C++ and what little STL knowledge I once had.
I have come across a situation I have not encountered before using the IF
This is the situation. I had async call so I needed to make Mid

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.