|
ʹÓÃC++ÉîÈëÑо¿.NETίÍÐÓëʼþ |
|
| http://www.aguso.com¡¡2007-11-18 03:14:43¡¡¹ÈËÑIT·þÎñ |
|
|
¼ò½é
¡¡¡¡ÀàÐͰ²È«»úÖÆµÄʵÏÖÔÀ´²ÉÓõÄÊÇC·ç¸ñµÄ»Øµ÷£¨callback£©º¯Êý£¬¶ø.NET FrameworkÒýÈëÁËίÍкÍʼþÀ´Ìæ´úÔÀ´µÄ·½Ê½£»ËüÃDZ»¹ã·ºµØÊ¹Óá£ÎÒÃÇÔÚÕâÀï³¢ÊÔʹÓñê×¼C++À´ÊµÏÖÓëÖ®ÀàËÆµÄ¹¦ÄÜ£¬ÕâÑùÎÒÃDz»µ«¿ÉÒÔ¶ÔÕâЩ¸ÅÄîÓÐÒ»¸ö¸üºÃµÄÈÏʶ£¬¶øÇÒͬʱ»¹Äܹ»ÌåÑéC++µÄһЩÓÐȤµÄ¼¼Êõ¡£
¡¡¡¡C#ÖеÄίÍÐÓëʼþ¹Ø¼ü×Ö
¡¡¡¡Ê×ÏÈÎÒÃÇÀ´¿´Ò»¸ö¼òµ¥µÄC#³ÌÐò(ÏÂÃæµÄ´úÂëÂÔÓÐɾ½Ú)¡£Ö´ÐгÌÐòµÄÊä³ö½á¹ûÈçÏÂÏÔʾ£º
SimpleDelegateFunction called from Ob1,
string=Event fired!
Event fired!(Ob1): 3:49:46 PM on
Friday, May 10, 2002
Event fired!(Ob1): 1056318417
SimpleDelegateFunction called from Ob2,
string=Event fired!
Event fired!(Ob2): 3:49:46 PM on
Friday, May 10, 2002
Event fired!(Ob2): 1056318417
¡¡¡¡ËùÓÐÕâЩ¶¼Ô´ÓÚÕâÑùÒ»ÐдúÂ룺dae.FirePrintString("Event fired!");
¡¡¡¡ÔÚÀûÓÃC++À´ÊµÏÖÕâЩ¹¦ÄÜʱ£¬ÎÒÄ£·ÂÁËC#µÄÓï·¨²¢ÍêÈ«°´ÕÕ¹¦ÄܵÄÒªÇó½øÐпª·¢¡£
namespace DelegatesAndEvents { class DelegatesAndEvents { public delegate void PrintString(string s); public event PrintString MyPrintString; public void FirePrintString(string s) { if (MyPrintString != null)MyPrintString(s); } }
class TestDelegatesAndEvents { [STAThread] static void Main(string[] args) { DelegatesAndEvents dae =new DelegatesAndEvents(); MyDelegates d = new MyDelegates(); d.Name = "Ob1"; dae.MyPrintString +=new DelegatesAndEvents.PrintString(d.SimpleDelegateFunction); // ... more code similar to the // above few lines ... dae.FirePrintString("Event fired!"); } } class MyDelegates { // ... "Name" property omitted... public void SimpleDelegateFunction(string s) { Console.WriteLine("SimpleDelegateFunction called from {0}, string={1}", m_name, s); } // ... more methods ... } } |
|
| |
|
|