其實要改成真正的C#版,我們主要要做2件事, 一是吧CEventHandler放到外面,可以讓外部直接構造, 二是實現operator +=和operator -=, 下面是我的實現代碼: #pragma once #include <functional> #include <algorithm> #include <vector> #include <assert.h> namespace Common { typedef void* cookie_type; template<typename TR, typename T1, typename T2> class CEventHandler { public: typedef TR return_type; typedef T1 first_type; typedef T2 second_type; typedef std::function<return_type (first_type, second_type)> handler_type; CEventHandler(const CEventHandler& h) { _handler = h._handler; assert(_handler != nullptr); } CEventHandler(handler_type h) { _handler = h; assert(_handler != nullptr); } template<typename class_type, typename class_fun> CEventHandler(class_type* pThis, class_fun object_function) { using namespace std::placeholders; _handler = std::bind(object_function, pThis, _1, _2); assert(_handler != nullptr); } return_type operator()(first_type p1, second_type p2) { return_type ret = return_type(); assert(_handler != nullptr); if(_handler != nullptr) ret = _handler(p1, p2); return ret; } handler_type _handler; }; template<typename EventHandler> class CEvent { public: typedef EventHandler event_handler_type; typedef typename event_handler_type::return_type return_type; typedef typename event_handler_type::first_type first_type; typedef typename event_handler_type::second_type second_type; typedef typename event_handler_type::handler_type handler_type; ~CEvent() { Clear(); } return_type operator()(first_type p1, second_type p2) { return_type ret = return_type(); size_t size = _handlers.size(); for(auto p : _handlers) { ret = p->operator()(p1, p2); } return ret; } cookie_type AddHandler(const event_handler_type& h) { event_handler_type*p = new(nothrow) event_handler_type(h); if(p != nullptr) _handlers.push_back(p); return (cookie_type)p; } void RemoveHandler(cookie_type cookie) { event_handler_type* p = (event_handler_type*)cookie; auto itr = std::find(_handlers.begin(), _handlers.end(), p); if(itr != _handlers.end()) { _handlers.erase(itr); delete p; } else { assert(false); } } cookie_type operator += (const event_handler_type& pHandler) { return AddHandler(pHandler); } void operator -= (cookie_type cookie) { RemoveHandler(cookie); } void Clear() { if(!_handlers.empty()) { int n = _handlers.size(); std::for_each(_handlers.begin(), _handlers.end(), [](event_handler_type* p) { assert(p != nullptr); delete p; }); _handlers.clear(); } } private: std::vector<event_handler_type*> _handlers; }; } //Common 然後我們就可以這樣使用了: // EventTest.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include "event2.h" using namespace std; class CObjectX { }; class CClickEventArgs: public CObjectX { }; class CButton: public CObjectX { public: void FireClick() { CClickEventArgs args; OnClicked(this, args); } typedef Common::CEventHandler<int, CObjectX*, CClickEventArgs&> ButtonClickEventHandler; Common::CEvent<ButtonClickEventHandler> OnClicked; }; class CMyClass { public: int OnBtuttonClicked(CObjectX* pButton, CClickEventArgs& args) { cout << "CMyClass: Receive button clicked event" << endl; return 1; } }; int OnBtuttonClicked_C_fun(CObjectX* pButton, CClickEventArgs& args) { cout << "C Style Function: Receive button clicked event" << endl; return 1; } class CMyFunObj { public: int operator()(CObjectX* pButton, CClickEventArgs& args) { cout << "Functor: Receive button clicked event" << endl; return 1; } }; int _tmain(int argc, _TCHAR* argv[]) { CButton btn; CMyClass obj; Common::cookie_type c1 = btn.OnClicked += CButton::ButtonClickEventHandler(&obj, &CMyClass::OnBtuttonClicked); Common::cookie_type c2 = btn.OnClicked += CButton::ButtonClickEventHandler(OnBtuttonClicked_C_fun); CMyFunObj functor; Common::cookie_type c3 = btn.OnClicked += CButton::ButtonClickEventHandler(functor); btn.FireClick(); btn.OnClicked -= c2; std::cout << endl; btn.FireClick(); system("pause"); return 0; } 怎麼樣,是不是感覺和C#一樣了 ! 最後,我們比較一下2種實現方式: 第一種方法把委托函數類型封裝起來了,對外部來說是透明的, 使用起來更簡單。 第二種方式把委托函數的類型暴露了出來, 適用於事件處理函數類型各異,比較強調事件處理函數類型的場合。