Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 608
Sinatr Avatar asked Jun 01 '26 04:06

Sinatr


1 Answers

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
like image 71
ilkerkaran Avatar answered Jun 03 '26 16:06

ilkerkaran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!