[Solved] Virtual void not being overridden with override keyword in C# [closed]


Your Debug.CallObjectEvent() method explicitly instantiates a Call object and calls the overridden method in that class:

public static class Debug
{
    internal static void CallObjectEvent(string log)
    {
        new Call().CallEvent(new Log(log, Timer.GetTime()));
    }
}

The CallEvent() method in the Call class simply calls base.Event(), which resolves to IDebug.Event(). The Program.Event() override is never invoked because Program is not in the class hierarchy at the point of the call.

When you override a method or property, the override applies only to the class where it is defined (and all of its child classes, of course). Since Program isn’t a parent class of Call there’s no reason why its overrides would ever be referenced.

From what you’ve written it looks like you’re trying to set up a system for handling different log outputs depending on the program’s requirements. You need a way to register the appropriate log writer for your program. Something like:

public interface ILogWriter
{
    void Event(Log item);
}

private class DefaultLogWriter : ILogWriter
{
    public void Event(Log item)
    {
        Console.WriteLine($"[test] {item.Time} {item.Message}");
    }
}

internal static class Debug
{
    private static ILogWriter _writer = null;

    public static ILogWriter Writer 
    { 
        get
        {
            if (_writer == null)
                _writer = new DefaultLogWriter();
            return _writer;
        }
        set => _writer = value;
    }

    internal static void CallObjectEvent(string log)
    {
        Writer.Event(new Log(log, Timer.GetTime()));
    }
}

class Program
{
    private class MyLogWriter : ILogWriter
    {
        public void Event(Log item)
        {
            Console.WriteLine($"[MyLogWriter] {item.Time} {item.Message}");
        }
    }

    static void Main()
    {
        Debug.Writer = new MyLogWriter();

        Debug.CallObjectEvent("Test message.");
    }       
}

0

solved Virtual void not being overridden with override keyword in C# [closed]