How to override an event handler that is not marked as virtual?

10,900

Solution 1

You don't need to replace OnCreated, just create your own handler and pass it to the Created event.

public class LocalFolderWatcher : FileSystemWatcher, IWatcher
{
    //...
    public event LocalFolderEventHandler CustomCreated;

    public LocalFolderWatcher()
    {
        Created += OnCreated;
    }

    private new void OnCreated(object sender, FileSystemEventArgs e)
    {
        string path = System.IO.Path.GetDirectoryName(e.FullPath);
        LocalFolderEventArgs args = new LocalFolderEventArgs(e.ChangeType, this._gatewayConfigurationName, path, this._folderConfigurationName, e.Name);

        if (CustomCreated != null && base.EnableRaisingEvents && base.Path != null)
            CustomCreated(this, args);
    }
}

Solution 2

You can't.

If you're trying to modify what the default handler does, forget it, you can't. If you want to add your own behaviour, then you can just create a new handler and subscribe the event.

Solution 3

Suppose we have the scenario

class BaseClass { public void M() { Console.WriteLine("BaseClass"); } }
class SubClass { public new void M() { Console.WriteLine("SubClass"); } }

void Main() {
    var x = new SubClass();
    x.M(); // Prints "SubClass"
    ((BaseClass)x).M(); // Prints "BaseClass"
}

In the first case, it calls SubClass.M and in the second it calls BaseClass.M. This is what "new" means - it creates a new method.

However, if we made BaseClass.M virtual and marked SubClass.M as override, then both of them would print "SubClass", because virtual calls check the runtime type of the caller. This explains why your event is never raised.

As IllidanS4 recommends, the best way is to add a listener to FileSystemWatcher.Created and make it call LocalFolderWatcher.

Share:
10,900
Ciccio
Author by

Ciccio

Updated on June 05, 2022

Comments

  • Ciccio
    Ciccio about 2 years

    I have to create a class that inherit from FileSystemWatcher. I should Intercept the event OnCreated and create my own event OnCreated. I have tried as follow:

    public class LocalFolderWatcher : FileSystemWatcher, IWatcher
    {
        ...
        public event LocalFolderEventHandler Created;
    
        protected override void OnCreated(FileSystemEventArgs e)
        {
            string path = System.IO.Path.GetDirectoryName(e.FullPath);
            LocalFolderEventArgs args = new LocalFolderEventArgs(e.ChangeType, this._gatewayConfigurationName, path, this._folderConfigurationName, e.Name);
    
            if (Created != null && base.EnableRaisingEvents && base.Path != null)
                Created(this, args);
    
            base.OnCreated(e);
        }
    }
    

    But I obtain an error: I cannot override a method that is not marked as virtual, abstract or override. I've tried to replace "override" with "new" but in this way the event is never raised..

    How can I intercept the real "OnCreated" and replace with mine?

    Thank you

  • Ciccio
    Ciccio over 9 years
    I'd like to have just one event "created".. In this way I have to create an handler for CustomeCreated. :( however, if there is no other solution, iz ok... thank you
  • IS4
    IS4 over 9 years
    Sure, you can also change it to public new event LocalFolderEventHandler Created; and then base.Created += OnCreated;. Then change every CustomCreated to Created.
  • Prageeth Liyanage
    Prageeth Liyanage over 3 years
    hi @IllidanS4wantsMonicaback with all due respect can you please check this question too. stackoverflow.com/questions/64238735/…
  • IS4
    IS4 over 3 years
    @Banjo I believe that question is already sufficiently answered.
  • Prageeth Liyanage
    Prageeth Liyanage over 3 years
    @IllidanS4supportsMonica Nope friend no bodies answer it correctly. What I need is to override current keypress event of the textbox and run my validation code plus need to run if there is any explicitly mentioned code inside key press events.