c#如何實(shí)現(xiàn)接口事件
接口可以聲明事件。 下面的示例演示如何在類中實(shí)現(xiàn)接口事件。 這些規(guī)則基本上都與實(shí)現(xiàn)任何接口方法或?qū)傩詴r(shí)的相同。
在類中實(shí)現(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);
}
}
}
示例
下面的示例演示如何處理不太常見(jiàn)的情況:類繼承自兩個(gè)或多個(gè)接口,且每個(gè)接口都具有相同名稱的事件。 在這種情況下,你必須為至少其中一個(gè)事件提供顯式接口實(shí)現(xiàn)。 為事件編寫顯式接口實(shí)現(xiàn)時(shí),還必須編寫 add 和 remove 事件訪問(wèn)器。 通常這些訪問(wèn)器由編譯器提供,但在這種情況下編譯器不提供它們。
通過(guò)提供自己的訪問(wèn)器,可以指定兩個(gè)事件是由類中的同一個(gè)事件表示,還是由不同事件表示。 例如,如果根據(jù)接口規(guī)范應(yīng)在不同時(shí)間引發(fā)事件,可以在類中將每個(gè)事件與單獨(dú)實(shí)現(xiàn)關(guān)聯(lián)。 在下面的示例中,訂閱服務(wù)器確定它們通過(guò)將形狀引用轉(zhuǎn)換為 IShape 或 IDrawingObject 接收哪個(gè) 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#如何實(shí)現(xiàn)接口事件的詳細(xì)內(nèi)容,更多關(guān)于c# 實(shí)現(xiàn)接口事件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#讀取txt文件數(shù)據(jù)的方法實(shí)例
讀取txt文本數(shù)據(jù)的內(nèi)容,是我們開發(fā)中經(jīng)常會(huì)遇到的一個(gè)功能,這篇文章主要給大家介紹了關(guān)于C#讀取txt文件數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2021-05-05
C#中把DataTable、Dataset轉(zhuǎn)Json數(shù)據(jù)
這篇文章介紹了C#中把DataTable、Dataset轉(zhuǎn)Json數(shù)據(jù)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
基于C#動(dòng)態(tài)生成帶參數(shù)的小程序二維碼
在微信小程序管理后臺(tái),我們可以生成下載標(biāo)準(zhǔn)的小程序二維碼,提供主程序入口功能,在實(shí)際應(yīng)用開發(fā)中,小程序二維碼是可以攜帶參數(shù)的,可以動(dòng)態(tài)進(jìn)行生成,本文小編就給大家介紹一下如何基于C#動(dòng)態(tài)生成帶參數(shù)的小程序二維碼,感興趣的朋友可以參考下2023-12-12
c#中directory 和directoryinfo的使用小結(jié)
當(dāng)使用C#處理目錄時(shí),可以使用?System.IO?命名空間中的?Directory?和?DirectoryInfo?類來(lái)執(zhí)行各種目錄操作,本文主要介紹了c#中directory 和directoryinfo的使用小結(jié),感興趣的可以了解一下2024-02-02

