摘要 class 事件 c#
abstract class event c#
我试图在我的摘要 class 和派生的 class 中引发事件,并让它们出现在 main 中的同一个事件中。这可能吗?使用下面的代码,我在两个 raise 事件中设置了一个断点,并且我看到了 bar raise 但从未看到 foo 并且由于 OnDataReceived 为 null 而从未调用 main 中的实际事件。我究竟做错了什么?如果我尝试在 bar 中使 bar 事件抽象和 Raise Function virtual 并在我的派生 classes 中重写这两者,那么我会得到一个错误,因为 bar 中的 OnDataReceived 不是 += 或 -= 的左侧。
这里基本上是我所拥有的:
public abstract class bar
{
public event DataReceivedHandler OnDataReceived;
protected void RaiseDataReceivedEvent(EventArgs e)
{
if (OnDataReceived != null)
OnDataReceived(this, e);
}
/// <summary>
/// A global event that will happen for all "bar" derived classes
/// </summary>
private void globalEvent()
{
//Raise this event for the derived class here.
RaiseDataReceivedEvent(new EventArgs());
}
}
public class foo : bar
{
public foo()
{
}
/// <summary>
/// A specific event that will happen only for this derived class
/// </summary>
private void fooSpecificEvent()
{
//Raise this event for the derived class here
RaiseDataReceivedEvent(new EventArgs());
}
}
public class Main
{
bar specificProduct = new foo();
public Main()
{
specificProduct.OnDataReceived += specificProduct_OnDataReceived;
}
void specificProduct_OnDataReceived(object sender, IttsDataReceivedEventArgs e)
{
//Here I want to process events from both fooSpecificEvent and globalEvent calls to RaiseDataReceivedEvent
}
}
您可以将方法 GlobalEvent() 更改为 protected 并简单地从 foo 特定事件中调用它。
类似的东西:
private void fooSpecificEvent()
{
//Raise this event for the derived class here
RaiseDataReceivedEvent(new EventArgs());
base.globalEvent();
}
所以,是的...上面的代码有效。只需确保调用的 foo 对象与连接到事件的 foo 对象相同。愚蠢的错误。
我有 foo_1.OnDataReceived 有线,但 foo_2 是事件调用。
我试图在我的摘要 class 和派生的 class 中引发事件,并让它们出现在 main 中的同一个事件中。这可能吗?使用下面的代码,我在两个 raise 事件中设置了一个断点,并且我看到了 bar raise 但从未看到 foo 并且由于 OnDataReceived 为 null 而从未调用 main 中的实际事件。我究竟做错了什么?如果我尝试在 bar 中使 bar 事件抽象和 Raise Function virtual 并在我的派生 classes 中重写这两者,那么我会得到一个错误,因为 bar 中的 OnDataReceived 不是 += 或 -= 的左侧。
这里基本上是我所拥有的:
public abstract class bar
{
public event DataReceivedHandler OnDataReceived;
protected void RaiseDataReceivedEvent(EventArgs e)
{
if (OnDataReceived != null)
OnDataReceived(this, e);
}
/// <summary>
/// A global event that will happen for all "bar" derived classes
/// </summary>
private void globalEvent()
{
//Raise this event for the derived class here.
RaiseDataReceivedEvent(new EventArgs());
}
}
public class foo : bar
{
public foo()
{
}
/// <summary>
/// A specific event that will happen only for this derived class
/// </summary>
private void fooSpecificEvent()
{
//Raise this event for the derived class here
RaiseDataReceivedEvent(new EventArgs());
}
}
public class Main
{
bar specificProduct = new foo();
public Main()
{
specificProduct.OnDataReceived += specificProduct_OnDataReceived;
}
void specificProduct_OnDataReceived(object sender, IttsDataReceivedEventArgs e)
{
//Here I want to process events from both fooSpecificEvent and globalEvent calls to RaiseDataReceivedEvent
}
}
您可以将方法 GlobalEvent() 更改为 protected 并简单地从 foo 特定事件中调用它。 类似的东西:
private void fooSpecificEvent()
{
//Raise this event for the derived class here
RaiseDataReceivedEvent(new EventArgs());
base.globalEvent();
}
所以,是的...上面的代码有效。只需确保调用的 foo 对象与连接到事件的 foo 对象相同。愚蠢的错误。
我有 foo_1.OnDataReceived 有线,但 foo_2 是事件调用。