Class1 具有属性 [EventPublication("event1")] 的事件。Class2 和 Class3 继承自 Class1。
我想使用 [EventSubscription] 将 Method1 订阅到来自 Class2 的对象中的事件,并将 Method2 订阅到来自 Class3 的对象中的事件。
但在派生类中,事件的 EventPublication 名称相同。那么如何区分派生类中的事件呢?可能吗?
编辑: 也许我误解了关于 IoC 的一些明显的事情,或者我试图使简单的解决方案复杂化......我会尝试澄清我的问题。这是一些代码:
class BasePresenter
{
[EventPublication("event")]
public event Action action;
public void Run()
{
someAction();
if (action != null)
action();
}
protected virtual void someAction()
{
}
}
class Presenter1 : BasePresenter
{
protected override void someAction()
{
}
}
class Presenter2 : BasePresenter
{
protected override void someAction()
{
}
}
class AnotherClass
{
[EventSubscription("event", ThreadOption.Caller)]
public void action1()
{
System.Windows.Forms.MessageBox.Show("Presenter1 started");
}
[EventSubscription("event", ThreadOption.Caller)]
public void action2()
{
System.Windows.Forms.MessageBox.Show("Presenter2 started");
}
}
另一个类中有 action1() 和 action2() 方法。我想在调用 Presenter1 Run() 方法的实例时触发 action1(),并在调用 Presenter2 Run() 方法的实例时触发 action2()。但是调用 Run() 方法会触发 action1 和 action2 方法。