最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c#如何實現(xiàn)接口事件

 更新時間:2020年10月13日 10:40:16   作者:olprod  
這篇文章主要介紹了c#如何實現(xiàn)接口事件,幫助大家更好的理解和學習c#,感興趣的朋友可以了解下

接口可以聲明事件。 下面的示例演示如何在類中實現(xiàn)接口事件。 這些規(guī)則基本上都與實現(xiàn)任何接口方法或屬性時的相同。

在類中實現(xiàn)接口事件

在類中聲明事件,然后在相應區(qū)域中調用它。

namespace ImplementInterfaceEvents 
{ 
 public interface IDrawingObject 
 { 
  event EventHandler ShapeChanged; 
 } 
 public class MyEventArgs : EventArgs
 { 
  // class members 
 } 
 public class Shape : IDrawingObject 
 { 
  public event EventHandler ShapeChanged; 
  void ChangeShape() 
  { 
   // Do something here before the event… 

   OnShapeChanged(new MyEventArgs(/*arguments*/)); 

   // or do something here after the event.
  } 
  protected virtual void OnShapeChanged(MyEventArgs e) 
  { 
   ShapeChanged?.Invoke(this, e); 
  } 
 } 

}

示例

下面的示例演示如何處理不太常見的情況:類繼承自兩個或多個接口,且每個接口都具有相同名稱的事件。 在這種情況下,你必須為至少其中一個事件提供顯式接口實現(xiàn)。 為事件編寫顯式接口實現(xiàn)時,還必須編寫 addremove 事件訪問器。 通常這些訪問器由編譯器提供,但在這種情況下編譯器不提供它們。

通過提供自己的訪問器,可以指定兩個事件是由類中的同一個事件表示,還是由不同事件表示。 例如,如果根據(jù)接口規(guī)范應在不同時間引發(fā)事件,可以在類中將每個事件與單獨實現(xiàn)關聯(lián)。 在下面的示例中,訂閱服務器確定它們通過將形狀引用轉換為 IShape IDrawingObject 接收哪個 OnDraw 事件。

namespace WrapTwoInterfaceEvents
{
 using System;

 public interface IDrawingObject
 {
  // Raise this event before drawing
  // the object.
  event EventHandler OnDraw;
 }
 public interface IShape
 {
  // Raise this event after drawing
  // the shape.
  event EventHandler OnDraw;
 }

 // Base class event publisher inherits two
 // interfaces, each with an OnDraw event
 public class Shape : IDrawingObject, IShape
 {
  // Create an event for each interface event
  event EventHandler PreDrawEvent;
  event EventHandler PostDrawEvent;

  object objectLock = new Object();

  // Explicit interface implementation required.
  // Associate IDrawingObject's event with
  // PreDrawEvent
  #region IDrawingObjectOnDraw
  event EventHandler IDrawingObject.OnDraw
  {
   add
   {
    lock (objectLock)
    {
     PreDrawEvent += value;
    }
   }
   remove
   {
    lock (objectLock)
    {
     PreDrawEvent -= value;
    }
   }
  }
  #endregion
  // Explicit interface implementation required.
  // Associate IShape's event with
  // PostDrawEvent
  event EventHandler IShape.OnDraw
  {
   add
   {
    lock (objectLock)
    {
     PostDrawEvent += value;
    }
   }
   remove
   {
    lock (objectLock)
    {
     PostDrawEvent -= value;
    }
   }
  }

  // For the sake of simplicity this one method
  // implements both interfaces.
  public void Draw()
  {
   // Raise IDrawingObject's event before the object is drawn.
   PreDrawEvent?.Invoke(this, EventArgs.Empty);

   Console.WriteLine("Drawing a shape.");

   // Raise IShape's event after the object is drawn.
   PostDrawEvent?.Invoke(this, EventArgs.Empty);
  }
 }
 public class Subscriber1
 {
  // References the shape object as an IDrawingObject
  public Subscriber1(Shape shape)
  {
   IDrawingObject d = (IDrawingObject)shape;
   d.OnDraw += d_OnDraw;
  }

  void d_OnDraw(object sender, EventArgs e)
  {
   Console.WriteLine("Sub1 receives the IDrawingObject event.");
  }
 }
 // References the shape object as an IShape
 public class Subscriber2
 {
  public Subscriber2(Shape shape)
  {
   IShape d = (IShape)shape;
   d.OnDraw += d_OnDraw;
  }

  void d_OnDraw(object sender, EventArgs e)
  {
   Console.WriteLine("Sub2 receives the IShape event.");
  }
 }

 public class Program
 {
  static void Main(string[] args)
  {
   Shape shape = new Shape();
   Subscriber1 sub = new Subscriber1(shape);
   Subscriber2 sub2 = new Subscriber2(shape);
   shape.Draw();

   // Keep the console window open in debug mode.
   System.Console.WriteLine("Press any key to exit.");
   System.Console.ReadKey();
  }
 }
}
/* Output:
 Sub1 receives the IDrawingObject event.
 Drawing a shape.
 Sub2 receives the IShape event.
*/

以上就是c#如何實現(xiàn)接口事件的詳細內容,更多關于c# 實現(xiàn)接口事件的資料請關注腳本之家其它相關文章!

相關文章

  • Unity制作自定義字體的兩種方法

    Unity制作自定義字體的兩種方法

    這篇文章主要為大家詳細介紹了Unity制作自定義字體的兩種方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • C#讀取txt文件數(shù)據(jù)的方法實例

    C#讀取txt文件數(shù)據(jù)的方法實例

    讀取txt文本數(shù)據(jù)的內容,是我們開發(fā)中經(jīng)常會遇到的一個功能,這篇文章主要給大家介紹了關于C#讀取txt文件數(shù)據(jù)的相關資料,需要的朋友可以參考下
    2021-05-05
  • C# zxing二維碼寫入的實例代碼

    C# zxing二維碼寫入的實例代碼

    C# zxing二維碼寫入的實例代碼,需要的朋友可以參考一下
    2013-03-03
  • 詳解C# 不能用于文件名的字符

    詳解C# 不能用于文件名的字符

    在 Windows 有一些字符是不能作為文件名,嘗試重命名一個文件,輸入/ 就可以看到windows 提示的不能作為文件名的字符,那么具體是包括哪些符號不能作為文件名呢?下面小編給大家介紹下
    2018-02-02
  • C#中把DataTable、Dataset轉Json數(shù)據(jù)

    C#中把DataTable、Dataset轉Json數(shù)據(jù)

    這篇文章介紹了C#中把DataTable、Dataset轉Json數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • C#中的串口通信SerialPort詳解

    C#中的串口通信SerialPort詳解

    今天這篇文章帶大家學習下C#中的串口通訊。在日常的開發(fā)工作中,如果工作內容是CS方向的同學應該很容易接觸到串口通訊方面的業(yè)務需求。那么也就很容易想到C#中SerialPort類,它就是專門來處理串口通訊相關的
    2022-01-01
  • 基于C#動態(tài)生成帶參數(shù)的小程序二維碼

    基于C#動態(tài)生成帶參數(shù)的小程序二維碼

    在微信小程序管理后臺,我們可以生成下載標準的小程序二維碼,提供主程序入口功能,在實際應用開發(fā)中,小程序二維碼是可以攜帶參數(shù)的,可以動態(tài)進行生成,本文小編就給大家介紹一下如何基于C#動態(tài)生成帶參數(shù)的小程序二維碼,感興趣的朋友可以參考下
    2023-12-12
  • C#實現(xiàn)單線程異步互斥鎖的示例代碼

    C#實現(xiàn)單線程異步互斥鎖的示例代碼

    異步互斥鎖的作用是用于確保存在異步操作的上下文同步互斥,這篇文章主要為大家詳細介紹了C#如何實現(xiàn)單線程異步互斥鎖,文中的示例代碼講解詳細,需要的可以參考下
    2024-01-01
  • Task提高異步執(zhí)行效率技巧

    Task提高異步執(zhí)行效率技巧

    這篇文章介紹了Task提高異步執(zhí)行效率的技巧,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • c#中directory 和directoryinfo的使用小結

    c#中directory 和directoryinfo的使用小結

    當使用C#處理目錄時,可以使用?System.IO?命名空間中的?Directory?和?DirectoryInfo?類來執(zhí)行各種目錄操作,本文主要介紹了c#中directory 和directoryinfo的使用小結,感興趣的可以了解一下
    2024-02-02

最新評論

绥江县| 宜都市| 安岳县| 迭部县| 武山县| 商水县| 墨脱县| 霸州市| 兴城市| 海安县| 津南区| 库尔勒市| 开远市| 阜新市| 阜新市| 宁南县| 黔西县| 武汉市| 温州市| 高尔夫| 嵩明县| 霍州市| 阳新县| 丰顺县| 米泉市| 临武县| 德保县| 松溪县| 班玛县| 黄骅市| 安仁县| 高要市| 通道| 尤溪县| 格尔木市| 青铜峡市| 陆丰市| 沈丘县| 光泽县| 峨山| 巴中市|