关于您的示例,我尝试了各种组合。这是唯一的工作代码。似乎您需要知道事件在继承层次结构中的具体定义位置。
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;
}
}
到目前为止的整体组合;
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