Search code examples
c#eventsweakeventmanager

Event not found on type


I have following exception

An unhandled exception of type 'System.ArgumentException' occurred in WindowsBase.dll

Additional information: 'Event' event not found on type 'ConsoleApplication.ITest'.

in this repro:

using System.Windows; // add reference to WindowsBase

interface IBase
{
    event EventHandler Event;
}

interface ITest : IBase { }

class Test : ITest
{
    public event EventHandler Event;
}

class Program
{
    static void Main(string[] args)
    {
        var test = new Test();
        WeakEventManager<IBase, EventArgs>.AddHandler(test, "Event", (s, e) => { }); // works
        WeakEventManager<ITest, EventArgs>.AddHandler(test, "Event", (s, e) => { }); // exception
    }
}

Why inherited via interfaces event can't be found? Exception is thrown from this method.


Solution

  • I've tried various combinations about your example. Here comes the only working code down here. It seems you need to know where the event is defined specificaly in the inheritance hierarchy.

    using System.Reflection;
    using MS.Internal.WindowsBase;
    
    namespace EventTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                var test = new Test();
                WeakEventManager<IBase, EventArgs>.AddHandler(test, "IBaseEvent", (s, e) => { }); // works
                WeakEventManager<ITest, EventArgs>.AddHandler(test, "ITestEvent", (s, e) => { }); // works 
                WeakEventManager<Test, EventArgs>.AddHandler(test, "TestEvent", (s, e) => { }); // works
            }
        }
        interface IBase
        {
            event EventHandler IBaseEvent;
        }
    
        interface ITest : IBase { event EventHandler ITestEvent; }
    
        class Test : ITest
        {        
            public event EventHandler IBaseEvent;
            public event EventHandler ITestEvent;
            public event EventHandler TestEvent;
        }
    }
    

    Whole combinations so far ;

                WeakEventManager<IBase, EventArgs>.AddHandler(test, "IBaseEvent", (s, e) => { }); // works
                WeakEventManager<IBase, EventArgs>.AddHandler(test, "ITestEvent", (s, e) => { }); // exception 
                WeakEventManager<IBase, EventArgs>.AddHandler(test, "TestEvent", (s, e) => { }); // exception
    
                WeakEventManager<ITest, EventArgs>.AddHandler(test, "IBaseEvent", (s, e) => { }); // exception
                WeakEventManager<ITest, EventArgs>.AddHandler(test, "ITestEvent", (s, e) => { }); // works 
                WeakEventManager<ITest, EventArgs>.AddHandler(test, "TestEvent", (s, e) => { }); // exception
    
                WeakEventManager<Test, EventArgs>.AddHandler(test, "IBaseEvent", (s, e) => { }); // exception
                WeakEventManager<Test, EventArgs>.AddHandler(test, "ITestEvent", (s, e) => { }); // exception 
                WeakEventManager<Test, EventArgs>.AddHandler(test, "TestEvent", (s, e) => { }); // works