3

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.

Community
  • 1
  • 1
Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • Looks like the WeakEventManager looks only for events **specifically** in the specified type - when I have declared another event in ITest interface and tried to add it - it worked just fine. It seems you need to provide the top-most (in inheritance hierarchy) type in order to find the event. But **why** is that so - I fall short here. – Jakub Jankowski Jul 14 '16 at 11:57
  • @jakub-jankowski, it seems like a bug, because `typeof(ITest).GetEvent("Event")` works – Sinatr Jul 14 '16 at 12:08

1 Answers1

6

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
ilkerkaran
  • 4,214
  • 3
  • 27
  • 42