事件上执行错误的派生类方法?
public Class A
{
public A()
{
someotherclass.someevent += new EventHandler(HandleEvent);
}
private void HandleEvent(object sender,CustomEventArgs e)
{
if(e.Name == "Type1")
Method1();
else if(e.Name == "Type2")
Method2();
}
protected virtual void Method1(){}
protected virtual void Method2(){}
}
public class B: A
{
public B()
{ /*Something*/}
protected override void Method1(){/*some logic*/}
protected override void Method2(){/*some other logic*/}
}
public class C: A
{
public C()
{ /*Something*/}
protected override void Method1(){/*some logic*/}
protected override void Method2(){/*some other logic*/}
}
public class Main
{
private A;
public Main(){/*Something*/}
private void StartB()
{
A = new B();
}
private void StartC()
{
A = new C();
}
}
现在,发生的情况是,在我经历了调用方法 StartB(称为第一个)和 StartC(称为第二个)的循环之后,当 someevent 被触发,代码尝试执行 B 类 中的方法(以及后来的 C 类,我希望如此。我无法到达那里,因为它在调用方法时出错在 B 类中),而不是我希望它只调用该方法在C类中。
我认为,由于事件是在构造函数中订阅的,B 类方法仍然会被触发,因为它最初是在调用 StartB 时订阅的。
问题:
我只想执行最新实例化的类的方法。
例如:如果按顺序调用StartB和StartC,则当someevent被触发时,C类中的方法应该只会被处决。反之亦然。怎么做呢?
我知道我正在做一些非常错误的事情。非常感谢任何帮助。
public Class A
{
public A()
{
someotherclass.someevent += new EventHandler(HandleEvent);
}
private void HandleEvent(object sender,CustomEventArgs e)
{
if(e.Name == "Type1")
Method1();
else if(e.Name == "Type2")
Method2();
}
protected virtual void Method1(){}
protected virtual void Method2(){}
}
public class B: A
{
public B()
{ /*Something*/}
protected override void Method1(){/*some logic*/}
protected override void Method2(){/*some other logic*/}
}
public class C: A
{
public C()
{ /*Something*/}
protected override void Method1(){/*some logic*/}
protected override void Method2(){/*some other logic*/}
}
public class Main
{
private A;
public Main(){/*Something*/}
private void StartB()
{
A = new B();
}
private void StartC()
{
A = new C();
}
}
Now, what happens is, after I go through a cycle in which both the methods StartB(called first) and StartC(called second) are called, when the someevent is triggered, the code tries to execute the Method in Class B(and later Class C, I hope. I could not get there since it errors out when it calls method in Class B), instead which I want it to call only the method in Class C.
I think that, since the event is subscribed at constructor, Class B methods are still getting fired since it is subscribed initially on the call of StartB.
Question:
I want only the methods of the class that is instantiated the latest should be executed.
For Example: if StartB and StartC are called in order, when someevent is triggered the Methods in Class C should only get executed. Same Vice-Versa. How to do that?
I know am doing something terribly wrong. Any help is much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有从第一个实例中取消订阅该事件,因此它将被调用。如果您不希望它被调用,您需要取消订阅。你可以做这样的事情
,但看起来很老套。您最好实现一种方法(例如
IDisposable
)来在创建新实例之前清理最后一个实例。You aren't unsubscribing from the event from your first instance so it will be called. If you don't want it to be called you need to unsubscribe. You could do something like this
but it seems pretty hacky. You are probably better off implementing a method (e.g.
IDisposable
) to clean up your last instance before a creating a new one.如果我理解正确的话,你是说在调用 startC 之后调用 B 上的方法,而你不希望这种情况发生?
我猜你的问题是 someotherclass 是一个静态类,或者一个实例以某种方式在所有创建的 B 和 C 之间共享 - 在这种情况下,你需要在创建新类时从 someotherclass.someevent 取消注册旧的事件处理程序。如果您不取消注册处理程序,则 someotherclass 对象将拥有对向其注册的 B 或 C 对象的引用,因此即使您覆盖主类中的引用,该对象仍然通过事件,并且在事件触发时仍然被调用。
If I understand you correctly you are saying the methods on B are being called after startC is called and you don't wish this to happen?
I'm guessing your issue is that someotherclass is a static class, or an instance is somehow being shared between all the created B's and C's - in which case you need to unregister the old event handler from someotherclass.someevent when you create the new class. If you don't unregister the handler then the someotherclass object will have a reference to the B or C object that registered with it, so even though you are overwriting the reference in the main class the object is still kept alive by the reference in the event and is still being called when the event is triggered.