将基本事件的事件覆盖到应用程序和流程 c#

Overriding events of a base event to an application and sequence of flow c#

将事件从基础 class 传播到继承或实现应用程序的最佳方式或最佳实践是什么 class (并在基础上处理它们) - 因为我希望所有代码都从执行MyClientListener 的基础到 Winform ?

我有一个 [WCF DuplexClient] class,其他 [WCF ClientListener] class 将从中派生。我想让它可重复用于我的所有服务。我有一个事件 InnerChannel_Faulted - 在这个基础 class 我在基础 class 中有一个初始化器订阅事件并且基础 class 通常会处理这些事件WCF 方面的事情。我还希望能够让我的特定 ClientListener 实现能够提供额外的功能 - 这些事件的行为 - 主要用于 Winforms 应用程序。

我的想法是否正确 - 或者我是否需要在食物链上反省事件以便 Winforms 应用程序可以使用它们?

我在基础 class 中制作了这样的处理程序:

    class MyClient<T> :DuplexClientBase<T> where T : class
    {
    protected virtual void InitializeClient()
    {
          base.InnerChannel.Faulted += InnerChannel_Faulted;
    }

     protected virtual void InnerChannel_Faulted(object sender, EventArgs e)
    {
     // ... do something()
    }

    }

class MyListener :  MyClient<MyListenerService>
{

public MyListener(){ // do stuff}
// .. other methods

}

WINDOWFORMAPP : FORM
{


  private MyListener mylistener = new MyListener();

  WINDOWFORMAPP()
{
// somehow subscribe to 
mylistener.InnerChannel_Faulted += 

}

  private override void InnerChannel_Faulted(object sender, EventArgs e)
{
 // DoSomething to GUI - notifications GUI elements etc.. 
// then call.
  mylistener.InnerChannel_Faulted()
}

}

从同一个class或子class订阅事件是不标准的。通常的方法是将代码结构化为:

public class MyClass {
   public event EventHandler SomeAction;

   private void DoStuff() {
       bool fireAction = false;
       //....
       if (fireAction) {
          EventArgs e = ...; // can be more specific if needed
          OnSomeAction(e);
       }
   }

   protected virtual void OnSomeAction(EventArgs e) {
     if (SomeAction != null)
         SomeAction(this, e);
   }
}

public class MySubclass : MyClass {
   protected override void OnSomeAction(EventArgs e) {
      // code before event is triggered
      base.OnSomeAction(e); // fires event to listeners
      // code after event is triggered
   }

}

然后在你的表单中:

public class MyForm : Form {

  MyClass mc = new MyClass();
  public MyForm() {
      mc.SomeAction += mc_SomeAction;
  }

  private void mc_SomeAction(Object sender, EventArgs e) {
     //...
  }
}