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.

2
  • 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. Jul 14, 2016 at 11:57
  • @jakub-jankowski, it seems like a bug, because typeof(ITest).GetEvent("Event") works
    – Sinatr
    Jul 14, 2016 at 12:08

1 Answer 1

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
2
  • I guess any other combination will fail?
    – Sinatr
    Jul 14, 2016 at 12:33
  • @Sinatr, i added all combinations that i tried to my answer since it was too long to comment =)
    – ilkerkaran
    Jul 14, 2016 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.