[英]Enforcing implementation of events in derived abstract classes
I am trying to create what I am thinking of as a translation class, so that my program can speak to a variety of target platforms. 我正在尝试创建我正在考虑的翻译类,以便我的程序可以与各种目标平台对话。 Each platform will be handled by a separate implementation of an abstract class. 每个平台都将由抽象类的单独实现处理。 I have pared things down for the sake of simplicity. 为了简单起见,我减少了一些东西。
I have an abstract class, with a couple of abstract methods: 我有一个抽象类,有几个抽象方法:
abstract class ControllerBase
{
public abstract bool EnableDTMFDetection(string CallID, Party Party);
public abstract bool DisableDTMFDetection(string CallID, Party Party);
}
Subsequently a class (classes) which derive from ControllerBase, and fully implement those methods: 随后是一个派生自ControllerBase的类(类),并完全实现这些方法:
class PlatformOne : ControllerBase
{
public override bool EnableDTMFDetection(string CallID, Party Party)
{
// Do Stuff
return true;
}
public override bool DisableDTMFDetection(string CallID, Party Party)
{
// Do Stuff
return true;
}
}
So far so good. 到现在为止还挺好。 For PlatformOne I am forced to define each of those methods, prescribing how I will send an outgoing message to my target platform. 对于PlatformOne,我被迫定义每个方法,规定如何将传出消息发送到目标平台。
It is the incoming events that have got me. 是我的传入事件。 I need to be able to raise events from within my derived classes. 我需要能够从派生类中引发事件。 When I add the following to controllerbase: 当我将以下内容添加到controllerbase时:
public delegate void MyEventHandler(object sender, EventArgs e);
public event MyEventHandler MyEvent;
It compiles fine, but I can't raise the event from within my derived class without the error: "The event 'ControllerBase.MyEvent' can only appear on the left hand side of += or -= (except when used from within the type 'ControllerBase')" 它编译得很好,但我不能在我的派生类中引发事件而没有错误:“事件'ControllerBase.MyEvent'只能出现在+ =或 - =的左侧(除非在输入'ControllerBase')“
So, a) How do I raise my event from within my derived class, and b) can anyone suggest a mechanism for enforcing the wiring up of specified events from within my derived class (a la abstract functions or interface methods). 那么,a)如何从我的派生类中引发我的事件,以及b)是否有人可以建议一种机制来强制从我的派生类(抽象函数或接口方法)中强制指定事件的连接。 Thank you call :) 谢谢你致电:)
The simplest way of doing this is to write a method in your base class to raise it: 最简单的方法是在基类中编写一个方法来提高它:
protected void OnMyEvent(EventArgs e)
{
// Note the copy to a local variable, so that we don't risk a
// NullReferenceException if another thread unsubscribes between the test and
// the invocation.
EventHandler handler = MyEvent;
if (handler != null)
{
handler(this, e);
}
}
You may want to make it a virtual method so you can override it in derived classes... it depends on your use case. 您可能希望将其设置为虚拟方法,以便可以在派生类中覆盖它...这取决于您的用例。
Note that this isn't forcing an implementation of anything in the derived class - but I think it's what you actually want. 请注意,这不是强制派生类中任何内容的实现 - 但我认为这是您真正想要的。
The standard pattern is to add an On<EventName>
virtual protected method to raise an event 标准模式是添加On<EventName>
虚拟保护方法来引发事件
protected virtual OnMyEvent(EventArgs e) {
var h = MyEvent;
if (h != null)
h(this, e);
}
Also bear in mind that events can be made abstract or virtual: 还要记住,事件可以是抽象的或虚拟的:
public abstract event MyEventHandler MyEvent;
although this is only generally used in interfaces, which might be better for your ControllerBase
depending on your exact requirements: 虽然这通常仅用于接口,但根据您的具体要求,这可能对您的ControllerBase
更好:
public interface IController {
event MyEventHandler MyEvent;
bool EnableDTMFDetection(string CallID, Party Party);
bool DisableDTMFDetection(string CallID, Party Party);
}
public PlatformOne : IControllerBase
// yadda yadda...
public event MyEventHandler MyEvent;
// members of PlatformOne can invoke MyEvent as a normal delegate
}
Create a protected event invocator in the abstract class. 在抽象类中创建受保护的事件调用程序。
protected void InvokeMyEvent(EventArgs args)
{
var handler = MyEvent;
if (hander != null)
handler(this, args);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.