c#的事件實現的原理

來源:互聯網
上載者:User

如果你看了本BLOG寫的委託的文章,理解本文應該比較容易,C#中的事件的實現是通過委託直接實現的,所以本文只給出事件實現的程式碼範例,如果你明白了委託,相信看懂本代碼應該不難。

 

using System;
namespace Events
{
        delegate void SomeDelegate(string sender);
        interface ISomeInterface
        {
                event SomeDelegate SomeEvent;
                void PerformSomeEvent();
                void SomeEventHandler(string sender);
        }
        abstract class AbstractClass:ISomeInterface
        {
                public virtual event SomeDelegate SomeEvent;
                public abstract void SomeEventHandler(string sender);
                public abstract void  PerformSomeEvent();
                protected void PerformSomeEventInternal(string sender)
                {
                        if(null!=SomeEvent)
                        {
                                SomeEvent(sender);
                         }
                 }
                protected void SomeEventHandlerInternal(string sender,string receiver)
                {
                        Console.WriteLine("some event has been handler");
                        Console.WriteLine(" Sender is ["+ sender+"]");
                        Console.WriteLine("Receiver is ["+receiver+"]");
                        Console.WriteLine("------------------------------------");
                 }       
         }   
        class SomeClass:AbstractClass,ISomeInterface
        {
                public override void PerformSomeEvent()
                {
                        PerformSomeEventInternal("Some Class");
                }
                public override void SomeEventHandler(string sender)
                {
                        SomeEventHandlerInternal(sender,"Some Class");
                 }       
        }  
        class AnotherClass:AbstractClass,ISomeInterface
        {
                public override void PerformSomeEvent()
                {
                        PerformSomeEventInternal("Another Class");
                }
                public override void SomeEventHandler(string sender)
                {
                        SomeEventHandlerInternal(sender,"Another Class");
                 }       
        }  
        class SomeApplication
        {
                public static void SomeEventStaticHander(string sender)
                {
                        Console.WriteLine("some event has been handler.");
                        Console.WriteLine(" Sender is ["+ sender+"]"); 
                        Console.WriteLine("Receiver is [some application]");
                        Console.WriteLine("-------------------------------------");
                 }
                static void Main(string [] args)
                {
                        SomeClass someclass=new SomeClass();
                        AnotherClass anotherclass=new AnotherClass();
                        someclass.SomeEvent+=new SomeDelegate(someclass.SomeEventHandler);
                        someclass.SomeEvent+=new SomeDelegate(anotherclass.SomeEventHandler);
                        someclass.SomeEvent+=new SomeDelegate(SomeApplication.SomeEventStaticHander);
                        someclass.PerformSomeEvent();
                        Console.ReadLine();
                 } 
         }
}

啟動並執行結果:

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.