亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

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

在類中聲明事件,然后在相應(yīng)區(qū)域中調(diào)用它。

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ī)范應(yīng)在不同時間引發(fā)事件,可以在類中將每個事件與單獨實現(xiàn)關(guān)聯(lián)。 在下面的示例中,訂閱服務(wù)器確定它們通過將形狀引用轉(zhuǎ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)接口事件的詳細(xì)內(nèi)容,更多關(guān)于c# 實現(xiàn)接口事件的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C#生成Word文件(圖片、文字)

    C#生成Word文件(圖片、文字)

    這篇文章主要為大家詳細(xì)介紹了C#生成Word文件,包括圖片、文字等素材,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • c#的時間日期操作示例分享(c#獲取當(dāng)前日期)

    c#的時間日期操作示例分享(c#獲取當(dāng)前日期)

    這篇文章主要介紹了c#的時間日期操作示例,在給定時間戳返回指定的時間格式和獲取當(dāng)前時間方法,需要的朋友可以參考下
    2014-03-03
  • C#擴(kuò)展方法實例分析

    C#擴(kuò)展方法實例分析

    這篇文章主要介紹了C#擴(kuò)展方法,結(jié)合實例形式分析了C#擴(kuò)展方法的功能、使用方法及相關(guān)注意事項,需要的朋友可以參考下
    2017-06-06
  • C#實現(xiàn)格式化SQL語句的示例代碼

    C#實現(xiàn)格式化SQL語句的示例代碼

    這篇文章主要為大家詳細(xì)介紹了C#如何實現(xiàn)格式化SQL語句的功能,文中的示例代碼簡潔易懂,具有一定的借鑒價值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-08-08
  • 詳細(xì)介紹C#之文件校驗工具的開發(fā)及問題

    詳細(xì)介紹C#之文件校驗工具的開發(fā)及問題

    目前校驗文件使用最多的是MD值和SHA值,不外乎有些使用CRC,前段時間微軟發(fā)布了VisualStudio正式版,win鏡像,微軟官方給出的校驗方式都是校驗文件的SHA值。下面詳細(xì)介紹C#之文件校驗工具的開發(fā)及問題,需要的朋友可以參考下
    2015-07-07
  • C# Windows API應(yīng)用之基于GetDesktopWindow獲得桌面所有窗口句柄的方法

    C# Windows API應(yīng)用之基于GetDesktopWindow獲得桌面所有窗口句柄的方法

    這篇文章主要介紹了C# Windows API應(yīng)用之基于GetDesktopWindow獲得桌面所有窗口句柄的方法,結(jié)合實例形式分析了GetDesktopWindow函數(shù)用于獲取窗口句柄的具體使用方法與相關(guān)注意事項,需要的朋友可以參考下
    2016-08-08
  • 輕松學(xué)習(xí)C#的ArrayList類

    輕松學(xué)習(xí)C#的ArrayList類

    輕松學(xué)習(xí)C#的ArrayList類,對C#的ArrayList類感興趣的朋友可以參考本篇文章,幫助大家更靈活的運用C#的ArrayList類
    2015-11-11
  • c#基于Redis實現(xiàn)輕量級消息組件的步驟

    c#基于Redis實現(xiàn)輕量級消息組件的步驟

    這篇文章主要介紹了c#基于Redis實現(xiàn)輕量級消息組件的步驟,幫助大家更好的理解和學(xué)習(xí)使用c#進(jìn)行開發(fā),感興趣的朋友可以了解下
    2021-05-05
  • C#如何快速釋放內(nèi)存的大數(shù)組詳解

    C#如何快速釋放內(nèi)存的大數(shù)組詳解

    這篇文章主要給大家介紹了關(guān)于C#如何快速釋放內(nèi)存的大數(shù)組的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • C#查找對象在ArrayList中出現(xiàn)位置的方法

    C#查找對象在ArrayList中出現(xiàn)位置的方法

    這篇文章主要介紹了C#查找對象在ArrayList中出現(xiàn)位置的方法,涉及C#中IndexOf方法的使用技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04

最新評論